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 sender
object is the one that's triggering the event, so we can just cast it to a ComboBox
and check if it's one that we care about:
private void BoardComboBo(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
if (comboBox == comboBox1)
{
// comboBox1 code here
}
else if (comboBox == comboBox12)
{
// comboBox12 code here
}
}
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 the Text
property of comboBox1
. Most likely, the Text
does not have any whitespace in it, so Split
is returning a single-item array. Then the problem is when you try to access an index that doesn't exist here:
split[1] // Here you're attempting to access an index that doesn't exist
To fix this issue, check the Length
of the array before accessing indexes that may not exist, maybe something like:
// Set the last name to an empty string if it didn't exist
var lastName = split.Length > 1 ? split[1] : string.Empty;
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 if
blocks instead of an if / else if
. This means that both the if
conditions will be evaluated regardless of which control triggered the event. It also means that if both ComboBoxes have a non-zero SelectedIndex
, then both the if
block 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 SelectedIndex
will be -1
, not 0
. This means that when you check for comboBox1.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
).