Search code examples
c#wpfxaml

ComboBox selection returning IndexOutofBounds Error


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.

enter image description here


Solution

  • 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