Link to home
Start Free TrialLog in
Avatar of G F
G FFlag for Canada

asked on

combobox Datasource issue clearing Items

Hi!

I'm working on a C# program, and was working through a Windows Form connected to back-end Maria DB.  Everything is going well, except for when I wanted to clear the form, which is a collection of Comboboxes, listboxes, a listview and Text Boxes.

I started with this command to clear a combobox:  this.comboBox5.Text = null;

But I noticed it just cleared the box, not the items listed, so I then used this command:   this.comboBox5.Items.Clear();

That threw me an error: System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.'


Looking on the internet, it said to verify there is no Datasource, and there isn't any, at least not in the menu for the combobox property.

So further looking , it suggested this command before clearing the items: this.comboBox5.DataSource = null;

Now, so this actually worked, it cleared the combo box and everything seemed okay.  Until I selected an item in Combobox1 (which feeds a list into Combobox5).
This is now a picture of what I get in Combobox5:


User generated image
I think the issue is in how the code is generated for Combobox5.

So to start this is the code for Combobox1, which keys off of the Selection Change Committed event:


 private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SetState("Start");
            comboBox5.Text = "";
            listView1.Items.Clear();
            listBox1.Items.Clear();
            set_Unit_form();
            this.txtBox_Unit_ID.Text = "";
           
            if (comboBox1.SelectedIndex != -1) { txtBox_CustomerID.Text = comboBox1.SelectedValue.ToString(); }
            load_Units_ComboBox();   //ComboBox5

        }



So at the end of that, it's calling a loadunitcombobox method, where I think the problem is, and this is the code for that:


private void load_Units_ComboBox()
        {

            int newIDtemp;
           
            int.TryParse(txtBox_CustomerID.Text, out newIDtemp);

            MySqlConnection con1 = new MySqlConnection(@"datasource = localhost; port=3306;username=root;pwd=;");
           
            con1.Open();
            MySqlCommand sc = new MySqlCommand("SELECT UnitNumber from bbb.units WHERE CustomerID = '" + newIDtemp + "' ORDER BY UnitNumber ASC", con1);
            MySqlDataReader reader;

            reader = sc.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("UnitNumber", typeof(string));
            dt.Load(reader);
            comboBox5.ValueMember = "UnitNumber";
            comboBox5.DataSource = dt;

            con1.Close();
            con1.Dispose();

            comboBox5.SelectedIndex = -1;

        }



I use similar code to load Combobox1, including the name of the Datatable (dt), I didn't post that code.  Just wondering if that can be causing a conflict of some sort?


So that is where I'm at... every item has the same result.  It has the correct number, depending on what is selected from ComboBox1.  So very strange.
Just wondering if anyone has any suggestions on what I can try?

Thank you in advance!

G
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Try to clear the items after resetting the DataSource like :

this.comboBox.DataSource = null;
this.comboBox.Items.Clear();

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of G F

ASKER

Hi Zakaria and IT_saige,


Thank you for the reply.

Zakaria,

Yes, that is almost exactly my current code, this is how I'm trying to clear ComboBox5:

this.comboBox5.Text = null; 
this.comboBox5.DataSource = null; 
this.comboBox5.Items.Clear(); 

It's failing with this in place currently.

IT_saige, ok I'll look at my code and see what I can find.


Avatar of G F

ASKER

Hi it_saige,

I've changed my code to the following for combobox5:

reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("UnitNumber", typeof(string));
dt.Load(reader);
comboBox5.ValueMember = "UnitNumber";
comboBox5.DisplayMember = "UnitNumber";
comboBox5.DataSource = dt;


and it looks like it now works!

Thank you!