private void btnChange_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update Customer set MembershipPoint='" + textMembershipPoint.Text + "' where NameCustomer='" + textNameCustomer.Text + "'";
cmd.ExecuteNonQuery();
if (cmd.ExecuteScalar() != null)
{
textMembershipPoint.Text = Convert.ToString(cmd.ExecuteScalar());
}
else if (cmd.ExecuteScalar() != )
{
MessageBox.Show("Invalid Name of Customer.");
}
else if (cmd.ExecuteScalar() != )
{
MessageBox.Show("Invalid Membership Point. Only Number Allowed.");
}
else
{
MessageBox.Show("Membership Point is changed.");
}
con.Close();
display_data();
}
I have a database table called Customer with columns ID_Customer, NameCustomer and MembershipPoint.
When a customer inputs a name that is not in the Customer table, the output will show "Invalid Name of Customer.".
If customer input an invalid MembershipPoint, the output will show "Invalid Membership Point. Only Number Allowed.".
If all else is good, then the output will show "Membership Point is changed.".
Can anyone tell me what I need to do right for the if else statement in order to achieve that?
.ExecuteNonQuery(), and then potentially several times withExecuteScalar- that's definitely a big NO-NO! Execute it once and get the information you need - don't keep executing it over and over again!