There's an error saying that the combobox is already b, inded to a datasource, thus cannot add new items.

This is how I populate my combobox

cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_distinctevent";
            cmd.Connection = con;

            da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            ds = new DataSet();
            da.Fill(ds, "tblVisitorVisit");

            purposecb.DisplayMember = "eventName";
            purposecb.ValueMember = "visitorVisitID";
            purposecb.DataSource = ds.Tables["tblVisitorVisit"];

purposecb.DataSource = null, Try this before assigning a new datasource

i.e
purposecb.DataSource = null;
purposecb.DataSource = ds.Tables["tblVisitorVisit"];
purposecb.DisplayMember = "eventName";
purposecb.ValueMember = "visitorVisitID";

What I have done in the past is to create a datatable which you populate and manage yourself and then bind to that datatable. If you need to add items to the list then you add extra rows to the datatable and then refresh the contents of the list box / combo.

use

purposecb.Items.Clear()

property for every time u r setting the datasource property of the combobox.

Hope it will Help U.

Regards

Kalyan

There's an error saying that the combobox is already b, inded to a datasource, thus cannot add new items.

Add items to a dataSource, not to comboBox.
When you have some control bind to a data source, you have to work then with the data source (inserting, updating, removing), not with the control.

Mitja

I tried working with the dataset by adding a new row.

ds.Tables["table"].Rows.Add("- All -");

It works.. Anyway, what's the difference of setting the datasource to null before assigning to a new data source?

To null, that means the dataTable is empty and it has no name (if you have specified it before filling it). So, it clears out.
Its like the same as you do:

DataTable table = new DataTable();

Mitja

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.