troubleshooting Question

combobox Datasource issue clearing Items

Avatar of G F
G FFlag for Canada asked on
MySQL Server.NET ProgrammingC#Windows OS
4 Comments1 Solution45 ViewsLast Modified:
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:


Error in dropdown Combobox
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros