Reputation: 3651
I am setting up such that the choice of my first combobox(name = combo3) determines what the second combobox(name = combo4) will show. This works fine.
Now I am trying to make the second combobox determine what my third combobox will show. This doesn't work. The moment I make a selection on the second combox, it jams and returns error. But if I hard code the value of the 2nd combobox, it works.
I am getting indexoutofbounds exception. Problem seems to be with the method private void ComboBox_SelectionChanged2(). Please advice what is wrong. Thanks.
MainWindow
string[] tableArray = { "Agent_Name", "Agent_Age", "Agent_Gender", "Agent_Alias", "Security_Clearance", "Dept_ID", "Job_ID", "Mission_ID" };
string[] attributeArray = { "Mission_Name", "Mission_Description", "Mission_Country", "Mission_City" };
private void ComboBox_SelectionChanged1(object sender, SelectionChangedEventArgs e)
{
if (((ComboBoxItem)combo3.SelectedItem).Content.ToString() == "Agents")
{
combo4.Items.Clear();
foreach (string x in tableArray)
{
combo4.Items.Add(x);
}
}
else
{
combo4.Items.Clear();
foreach (string x in attributeArray)
{
combo4.Items.Add(x);
}
}
}
private void ComboBox_SelectionChanged2(object sender, SelectionChangedEventArgs e)
{
combo5.Items.Clear();
MessageBox.Show(combo4.Text); //debugging line returns empty which shouldn't be the case. I chose Mission_Name thus it should show Mission_Name.
//fillCombo("SELECT * FROM " + combo3.Text + ";", "Mission_Name", combo5); //works
fillCombo("SELECT * FROM " + combo3.Text + ";", combo4.Text, combo5); // not working
}
private void fillCombo(string query, string name, ComboBox c)
{
MySqlCommand cmdReader = new MySqlCommand(query, conn);
MySqlDataReader myReader;
myReader = cmdReader.ExecuteReader();
while (myReader.Read())
{
string temp = myReader.GetString(name);
c.Items.Add(temp);
}
myReader.Close();
}
XAML:
<ComboBox x:Name="combo3" Width="120" SelectionChanged="ComboBox_SelectionChanged1">
<ComboBoxItem x:Name="box3" Content="Agents"/>
<ComboBoxItem x:Name="box4" Content="Missions"/>
</ComboBox>
<ComboBox x:Name="combo4" Width="120" SelectionChanged="ComboBox_SelectionChanged2">
</ComboBox>
<ComboBox x:Name="combo5" Width="120" Canvas.Left="818" Canvas.Top="588"/>
Error Message:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in MySql.Data.dll
Additional information: Could not find specified column in results:
How the combo boxes looks.
Upvotes: 0
Views: 361
Reputation: 3683
Replace this
MessageBox.Show(combo4.Text);
With this:
MessageBox.Show(combo4.SelectedText);
Or with this:
MessageBox.Show(combo4.SelectedItem.ToString());
replace everywhere you use combo4.Text as-well as combo1 2 3 and so on
Upvotes: 1
Reputation: 3314
The problem is that you are using the ComboBox_SelectionChanged2
event to run the code to fill out your comboboxes, but you aren't validating that the comboboxes have selections made before running the code. This event will be triggered when the combobox is painted on the form with no value selected in some of the other comboboxes. You just need to put a conditional in your event that ensures that your comboboxes have values before trying to access them.
private void ComboBox_SelectionChanged2(object sender, SelectionChangedEventArgs e)
{
if (combo3.Text.Length > 0 && combo4.Text.Length > 0)
{
combo5.Items.Clear();
MessageBox.Show(combo4.Text); //debugging line returns empty which shouldn't be the case. I chose Mission_Name thus it should show Mission_Name.
//fillCombo("SELECT * FROM " + combo3.Text + ";", "Mission_Name", combo5); //works
fillCombo("SELECT * FROM " + combo3.Text + ";", combo4.Text, combo5); // not working
}
}
Upvotes: 0
Reputation: 149538
As i said in the comments, there is a problem with the string being generated to query that database.
Try debugging your application and set a breakpoint to your combobox4.Text property and see what it generates.
Upvotes: 0