so my program goes like this:

combobox1 controls conbobox2, so if I choose an item in combobox1, combobox2 will be populated by my query from my database.

the problem is that, whenever i try to choose another item from combobox1, combobox2 will only stack the items from the database, and does not clear the items from previous selection.

all i want is that, whenever i choose a new item from combobox1 combobox2 will update its contents (deleting the previous items and populating it with the new items from my database)

i tried "this.combobox2.Items.Clear();" but the error "Items collection cannot be modified when the DataSource property is set." will show up.

heres my code:

private void comboUnitType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboUnitType.SelectedIndex.Equals(0))
            {
                this.comboUnitAvailable.Show();
                this.comboUnitAvailable.Items.Clear();

                Connect = new SqlConnection(sqlConnStatement);
                Connect.Open();
                daAddReserve = new SqlDataAdapter(
                    "SELECT Equipment_Name FROM Equipment WHERE Equipment_Type = 'CPU'", Connect);
                
                cbAddReserve = new SqlCommandBuilder(daAddReserve);
                daAddReserve.Fill(dsLCD, "Equipment");
                comboUnitAvailable.DataSource = dsLCD.Tables[0];
                comboUnitAvailable.DisplayMember = "Equipment_Name";
                Connect.Close();

            }
            
            
            else if (comboUnitType.SelectedIndex.Equals(1))
            {
                this.comboUnitAvailable.Show();
                this.comboUnitAvailable.Items.Clear();

                
                                
                Connect = new SqlConnection(sqlConnStatement);
                Connect.Open();
                daAddReserve = new SqlDataAdapter(
                    "SELECT Equipment_Name FROM Equipment WHERE Equipment_Type = 'Laptop'", Connect);

                cbAddReserve = new SqlCommandBuilder(daAddReserve);
                daAddReserve.Fill(dsLCD, "Equipment");
                comboUnitAvailable.DataSource = dsLCD.Tables[0];
                comboUnitAvailable.DisplayMember = "Equipment_Name";
                Connect.Close();
            }
            else if (comboUnitType.SelectedIndex.Equals(2))
            {
                this.comboUnitAvailable.Show();
                this.comboUnitAvailable.Items.Clear();

                
                Connect = new SqlConnection(sqlConnStatement);
                Connect.Open();
                daAddReserve = new SqlDataAdapter(
                    "SELECT Equipment_Name FROM Equipment WHERE Equipment_Type = 'OHP'", Connect);

                cbAddReserve = new SqlCommandBuilder(daAddReserve);
                daAddReserve.Fill(dsLCD, "Equipment");
                comboUnitAvailable.DataSource = dsLCD.Tables[0];
                comboUnitAvailable.DisplayMember = "Equipment_Name";
                Connect.Close();
            }

thanks in advance.

Have you tried setting the .DataSource of the combobox to null, then clearing it, then binding it? Also, there is probably a more elegant way to write that function, since all 3 if blocks are identical except for the where clause.

Dear

in first you should know that you bind the combobox to dataset, then you should deal with the dataSet in clearing and filling it.

DataAdapter.Fill method adds rows incase of the table is created in DataSet, so everytime you select from first combobox then data adapter add the new rows to the table ("Equipment") .

The solution :
add the next code to clear the rows:

if (dsLCD.Tables.Count > 0 && dsLCD.Tables[0].Rows.Count > 0)
                    dsLCD.Tables[0].Rows.Clear();

There is another thing :
remove this line from the code because this will give you runtime error , no need for this line now:

this.comboUnitAvailable.Items.Clear();
This article has been dead for over six months. Start a new discussion instead.