I am trying to update database table according to two dropdownlists value,
and I want to prevent the user from leaving any of the two dropdownlists empty
but the problem is it only check the first dropdownlist if it was empty
for example if I fill the first dropdownlist and left the second one empty it will update the update database table and will not prevent the user from leaving the second one empty
if(DropDownList_pc.SelectedItem.Value != "0" || DropDownList_laptop.SelectedItem.Value != "0")
if (DropDownList_pc.SelectedItem.Value == "1")
{
string sql = "update users_table set user_pc = 'yes'";
SqlCommand cmd1 = new SqlCommand(sql, sqlCon);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "done');", true);
}
else if (DropDownList_pc.SelectedItem.Value == "2")
{
string sql = "update users_table set user_pc = 'no'";
SqlCommand cmd1 = new SqlCommand(sql, sqlCon);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "done');", true);
}
if (DropDownList_laptop.SelectedItem.Value == "1")
{
string sql = "update users_table set user_laptop = 'yes'";
SqlCommand cmd1 = new SqlCommand(sql, sqlCon);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "done');", true);
}
else if (DropDownList_laptop.SelectedItem.Value == "2")
{
string sql = "update users_table set user_laptop = 'no'";
SqlCommand cmd1 = new SqlCommand(sql, sqlCon);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "done');", true);
}
}
else{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "you have to fill all feilds');", true);
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If both of your dropdowns doesn’t depend on each other you just need to fix your condition to use the AND operator like this:
if(DropDownList_pc.SelectedItem.Value != "0" && DropDownList_laptop.SelectedItem.Value != "0")
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0