Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I am trying to make a combo box dependant on a selection from another combobox.
Each are connected to a mysql server which is three tables.
what i want a is when you choose type it fills brand depending on the type. and once you choose brand do the same and fill the model. this is the code i got online but i implemented to work with me but it dosent . Can it be the tables?

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
             MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;username=root;password=Deprisa!79849;");
            //---comboBox2,3,and 9--- 
            
            comboBox3.SelectedIndex = -1;
            //comboBox3.Items.Clear();
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            if (comboBox2.SelectedValue != null)
            {
                DataView dv = comboBox2.DataSource as DataView;
                dv.RowFilter = "brand = " + comboBox2.SelectedValue.ToString();

                MySqlCommand cmd = new MySqlCommand("Select * from brand where idtype='" + comboBox2.SelectedItem + "'", con);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    comboBox2.Items.Add(rd[0]);
                }
                rd.Close();
                cmd.Dispose();
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;username=root;password=Deprisa!79849;");
            comboBox9.SelectedIndex = -1;
            //comboBox9.Items.Clear();

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            MySqlCommand cmd = new MySqlCommand("Select distinct(idbrand) from model where idtype='" + comboBox2.SelectedItem + "'", con);

            MySqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                comboBox9.Items.Add(rd[0]);
            }
            rd.Close();
            cmd.Dispose();
        }




</code>
Posted
Comments
[no name] 9-Sep-13 11:37am    
"Can it be the tables?", sure could be. It depends on what you mean by "doesn't work". We would have no idea what that means.
Raa_in 9-Sep-13 16:13pm    
how would i make the tables should i have three different ones or just one ?
[no name] 9-Sep-13 16:51pm    
You are assuming that I can read your mind to know what it is that you are trying to do. Just like telling us "does not work"...
Love 2 code 9-Sep-13 11:51am    
You could try using comboBox2.SelectedText instead of comboBox2.SelectedItem, this gives you the id as Text.
Raa_in 9-Sep-13 16:13pm    
okay thank you ill try it

1 solution

1. Show the table structure.
2. Replace selectedItem to SelectedText or SelectedItem.Value

Overall, place breakpoints and tell about any errors which you are facing. Check the value in the selected value, then see if there are any matching items being returned from the database at all..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900