current error: Object reference not set to an instance of an object
I was getting invalid cast exception error but i changed (int) to Convert.toInt32 and that went away.
I go to debug it and the on my button click and the code runs all the way through not stopping at any of my breakpoints. The object reference I pass in I get all the info no problem, pass in user and set it to currentUser to read the data of object. A record is inserted into my Customer Database BUT all of the info im passing IS NOT THERE so I know I am having some kind of problem with that which im still not sure of yet.
In the Combo box selection is when you go to "Yes" and click yes button with messagebox it inserts a New customer into the table.
On the edit profile / save profile button click i am just updating / adding the new data.
private void BtnProfileEdit_Click(object sender, RoutedEventArgs e)
{
EnableControls();
if (boolBtnPush == true)
{
if (BtnProfileEdit.Content.Equals("Save"))
{
if(boolIsCustomer == true)
{
if (TextboxFirstName.Text.Equals(""))
{
MessageBox.Show("You 'MUST' enter a First name,", "WARNING", MessageBoxButton.OK);
}
else
{
try
{
testcus = new Customer(currentUser.UserID, TextboxFirstName.Text,
TextboxLastName.Text, TextboxAddress.Text, TextboxCity.Text,
ComboboxState.SelectedValue.ToString(), TextboxZip.Text, TextEmailAddress.Text);
UsersDB.UpdateCustomer(testcus);
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
}
else
{
MessageBox.Show("You must select 'Yes' in combobox Customer\n" +
"to add customer data.", "UnAcceptable", MessageBoxButton.OK);
}
}
}
boolBtnPush = true;
BtnProfileEdit.Content = "Save";
}
private void Btntest_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(currentUser.ToString());
}
private void CboCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string cboValue = "";
if (CboCustomer.SelectedIndex > 0)
cboValue = ((ComboBoxItem)CboCustomer.SelectedItem).Content.ToString();
if(cboValue.Equals("Yes"))
{
boolIsCustomer = true;
User addCustomer = null;
Customer newCustomer = null;
MessageBoxResult result = MessageBox.Show("Updating database to customer status.",
"Customer Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
if (result == MessageBoxResult.Yes)
{
try
{
addCustomer = new User(currentUser.UserID, currentUser.Username,
currentUser.Password, currentUser.IsAdmin, currentUser.UserCreatedDate, boolIsCustomer);
UsersDB.UpdateCurrentUser(addCustomer);
newCustomer = new Customer(currentUser.UserID, currentUser.Username, null, null,
null, null, null, null);
UsersDB.CreateCustomer(newCustomer);
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
} }
}
public static int CreateCustomer(Customer customer)
{
string SQLcreateQuery = "INSERT INTO Customers (UserId, FirstName, LastName, Address, " +
"City, State, ZipCode, EmailAddress) VALUES(@id, @fn, @ln, @ad, @ci, @st, @zc, @ea)";
SqlCommand cmdCreate = new SqlCommand(SQLcreateQuery, connection);
cmdCreate.Parameters.AddWithValue("@id", customer.UserID);
cmdCreate.Parameters.AddWithValue("@fn", customer.FirstName ?? Convert.DBNull);
cmdCreate.Parameters.AddWithValue("@ln", customer.LastName ?? Convert.DBNull);
cmdCreate.Parameters.AddWithValue("@ad", customer.Address ?? Convert.DBNull);
cmdCreate.Parameters.AddWithValue("@ci", customer.City ?? Convert.DBNull);
cmdCreate.Parameters.AddWithValue("@st", customer.State ?? Convert.DBNull);
cmdCreate.Parameters.AddWithValue("@zc", customer.ZipCode ?? Convert.DBNull);
cmdCreate.Parameters.AddWithValue("@ea", customer.EmailAddress ?? Convert.DBNull);
try
{
connection.Open();
cmdCreate.ExecuteNonQuery();
string SQLselect = "SELECT @@IDENTITY FROM Customers";
SqlCommand cmdSELECT = new SqlCommand(SQLselect, connection);
int CustomerId = Convert.ToInt32(cmdSELECT.ExecuteScalar());
return CustomerId;
}
catch (Exception ex) { throw ex; }
finally { connection.Close(); }
}
public static Customer ReadCustomerById(int id)
{
string SQLreadQuery = "SELECT * FROM Customers WHERE UserId=@uid";
SqlCommand cmdRead = new SqlCommand(SQLreadQuery, connection);
cmdRead.Parameters.AddWithValue("@uid", id);
try
{
connection.Open();
SqlDataReader reader = cmdRead.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
Customer customer = new Customer()
{
UserId = Convert.ToInt32(reader["UserId"]),
FirstName = reader["FirstName"].ToString(),
LastName = reader["LastName"].ToString(),
Address = reader["Address"].ToString(),
City = reader["City"].ToString(),
State = reader["State"].ToString(),
ZipCode = reader["ZipCode"].ToString(),
EmailAddress = reader["EmailAddress"].ToString()
};
return customer;
}
else
return null;
}
catch(Exception ex) { throw ex; }
finally { connection.Close(); }
}
public static bool UpdateCustomer(Customer customer)
{
bool result = false;
string SQLupdateQuery = "UPDATE Customers SET FirstName=@fn, LastName=@ln, " +
"Address=@ad, City=@ci, State=@st, ZipCode=@zc, EmailAddress=@ea " +
"WHERE UserId=@cid";
SqlCommand cmdUpdate = new SqlCommand(SQLupdateQuery, connection);
cmdUpdate.Parameters.AddWithValue("@cid", customer.UserId);
cmdUpdate.Parameters.AddWithValue("@fn", customer.FirstName);
cmdUpdate.Parameters.AddWithValue("@ln", customer.LastName);
cmdUpdate.Parameters.AddWithValue("@ad", customer.Address);
cmdUpdate.Parameters.AddWithValue("@ci", customer.City);
cmdUpdate.Parameters.AddWithValue("@st", customer.State);
cmdUpdate.Parameters.AddWithValue("@zc", customer.ZipCode);
cmdUpdate.Parameters.AddWithValue("@ea", customer.EmailAddress);
try
{
connection.Open();
cmdUpdate.ExecuteNonQuery();
result = true;
}
catch(Exception ex)
{
ex.Message.ToString();
throw ex;
}
finally { connection.Close(); }
return result;
}
What I have tried:
debugging but the button click runs through and no breakpoints hit