943,670 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3827
  • C# RSS
Jun 6th, 2009
0

combo box keeps adding new items and doesnt delete previous items.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psychoangelo89 is offline Offline
8 posts
since Jun 2009
Jun 6th, 2009
0

Re: combo box keeps adding new items and doesnt delete previous items.

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.
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Jun 7th, 2009
0

Re: combo box keeps adding new items and doesnt delete previous items.

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:
C# Syntax (Toggle Plain Text)
  1. if (dsLCD.Tables.Count > 0 && dsLCD.Tables[0].Rows.Count > 0)
  2. 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:
C# Syntax (Toggle Plain Text)
  1. this.comboUnitAvailable.Items.Clear();
Reputation Points: 10
Solved Threads: 3
Light Poster
AmirBedair is offline Offline
26 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: design document
Next Thread in C# Forum Timeline: instantiate the generic type within the generic class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC