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"];

Dani AI

Generated

Short answer: don’t call ComboBox.Items.Add while the control is data-bound. Either add the row to the bound data source (DataTable / BindingSource / List) or unbind temporarily. Adding a DataRow to the DataTable that you bound will update the ComboBox automatically; using a BindingSource gives you control over insertion and position.

Example — insert an “All” row into the bound DataTable before or after binding (keeps it out of ComboBox.Items and lets the UI update automatically):

DataTable t = ds.Tables["tblVisitorVisit"];
DataRow r = t.NewRow();
r["eventName"] = "- All -";
r["visitorVisitID"] = DBNull.Value;   // or a sentinel like -1 if you need an int
t.Rows.InsertAt(r, 0);                 // puts it at the top of the list

Alternative — use a BindingSource (handy if you need Add/Insert at runtime and automatic list notifications):

var bs = new BindingSource { DataSource = ds.Tables["tblVisitorVisit"] };
purposecb.DisplayMember = "eventName";
purposecb.ValueMember = "visitorVisitID";
purposecb.DataSource = bs;

// to add a new row via the BindingSource
var drv = (DataRowView)bs.AddNew();
drv["eventName"] = "- All -";
drv["visitorVisitID"] = -1;
bs.EndEdit();
bs.Position = bs.IndexOf(drv);

Notes and troubleshooting: if you need that dummy row to persist to the database, handle it explicitly (DataAdapter.Update will try to insert whatever is in the table). If the dummy row is only for UI, use DBNull or a sentinel and filter it out before updating. Setting DataSource = null and using Items.Clear/Add (as and touched on) is possible but clunky — you lose automatic updates and must rebind. As and pointed out, work with the data source, not the control.

Recommended Answers

All 8 Replies

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";

Where would I put the adding of items?

Reading this may help.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.