I made a same event for 2 comboboxes to populate my input controls so the user can see what they would be deleting or updating. When I made separate events it worked but upon making the same event it does not work. What conditions should i put in the if-statements
on this if condition the error i get id "Index was outside the bounds of the array"
private void BoardComboBo(object sender, EventArgs e)
{
if (comboBox12.SelectedIndex!=0)//error is here
{
con.Open();
cmd = new SqlCommand("Select * from Users where user_Id='" + comboBox12.Text + "';", con);
SqlDataReader DR1 = cmd.ExecuteReader();
if (DR1.Read())
{
//code to fill textboxes
}
con.Close();
}
if(comboBox1.SelectedIndex!=0)//error is here
{
//to split combobox values
string selectedvalue = comboBox1.Text;
split = selectedvalue.Split();
//add values on fields
con.Open();
cmd = new SqlCommand("Select * from Users where firstName='" + split[0] + "' and lastName='" + split[1] + "';", con);
SqlDataReader DR1 = cmd.ExecuteReader();
if (DR1.Read())
{
//code to fill textboxes
}
con.Close();
}
}
I changed the second if condition too:
if (comboBox12.Text!=" ")// this works
{
//to split combobox values
string selectedvalue = comboBox1.Text;
split = selectedvalue.Split();
//add values on fields
con.Open();
cmd = new SqlCommand("Select * from Users where firstName='" + split[0] + "' and lastName='" + split[1] + "';", con);
SqlDataReader DR1 = cmd.ExecuteReader();
if (DR1.Read())
{
//code to fill textboxes
}
con.Close();
}
I am now getting the same error above when i try to select by user_Id
In the method signature, the
senderobject is the one that's triggering the event, so we can just cast it to aComboBoxand check if it's one that we care about:However at this point, you may as well have two separate events. Since there is no significant duplicated code that's specific to the ComboBoxes, refactoring them into one event only makes the code more cumbersome.
Regarding the
"Index was outside the bounds of the array"error, the only place you reference an index is when you split theTextproperty ofcomboBox1. Most likely, theTextdoes not have any whitespace in it, soSplitis returning a single-item array. Then the problem is when you try to access an index that doesn't exist here:To fix this issue, check the
Lengthof the array before accessing indexes that may not exist, maybe something like:Note that you should be using SQL command parameters to build the query string. The way you're doing it is vulnerable to SQL injection.
Also, as @Streamline mentioned, your original code has two
ifblocks instead of anif / else if. This means that both theifconditions will be evaluated regardless of which control triggered the event. It also means that if both ComboBoxes have a non-zeroSelectedIndex, then both theifblock bodies will run. This may be a cause for the error in the code from your question, and is not likely the intended behavior.Finally, as @OlivierJacot-Descombes mentioned, if no item is selected then
SelectedIndexwill be-1, not0. This means that when you check forcomboBox1.SelectedIndex!=0, not only are you ignoring the value in the first (0) position, but if no value is selected, then it will pass this condition (because the value in that case is-1).