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

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 4
Reputation: psychoangelo89 is an unknown quantity at this point 
Solved Threads: 0
psychoangelo89 psychoangelo89 is offline Offline
Newbie Poster

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

 
0
  #1
Jun 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 26
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

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

 
0
  #2
Jun 6th, 2009
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.
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: AmirBedair is an unknown quantity at this point 
Solved Threads: 3
AmirBedair AmirBedair is offline Offline
Light Poster

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

 
0
  #3
Jun 7th, 2009
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:
  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:
  1. this.comboUnitAvailable.Items.Clear();
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC