Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This causes two bindings in the collection to bind to the same property. Parameter name: binding error in c#?

I have tried to bind a datasource to a DevExpress.XtraEditors.LookupEdit at run-time. I tried this code, but am getting the following error:

This causes two bindings in the collection to bind to the same property. Parameter name: binding.

Here is my code:

// Create an adapter to load data from the "Customers" table.
OleDbDataAdapter testcustomers = new OleDbDataAdapter(
"SELECT CustomerId, Customername FROM Customer WHERE CompanyId =" + TXE_CompId.Text, connection);
DataSet ds = new DataSet();  // Create a dataset that will provide data from the database.
testcustomers.Fill(ds, "Customer");  // Load data from the "Customers" table to the dataset.
// A BindingSource for the "Customers" table.
BindingSource binding_cust = new BindingSource(ds, "Customer");

CBL_SelectCustomer.DataBindings.Add("EditValue", binding_cust, "CustomerId");  // getting error on this line
// Specify the data source to display in the dropdown.
CBL_SelectCustomer.Properties.DataSource = binding_cust;
// The field providing the editor's display text.
CBL_SelectCustomer.Properties.DisplayMember = "CustomerName";
// The field matching the edit value.
CBL_SelectCustomer.Properties.ValueMember = "CustomerId";

// Add two columns to the dropdown.
LookUpColumnInfoCollection coll = CBL_SelectCustomer.Properties.Columns;
// A column to display the ProductID field's values.
coll.Add(new LookUpColumnInfo("CustomerName", 0));

How can this error be fixed?

like image 583
Srihari Avatar asked Apr 23 '14 06:04

Srihari


2 Answers

Every Control can have only one binding at a time. It looks like you already have a binding to the textboxes before and now when you try to rebind it, it throws an error. You need to clear the old binding before adding a new one.

Clear binding first and then add it to the control:

CBL_SelectCustomer.DataBindings.Clear();
CBL_SelectCustomer.DataBindings.Add("EditValue", binding_cust, "CustomerId");
like image 199
Niranjan Singh Avatar answered Sep 20 '22 12:09

Niranjan Singh


Try this before binding:

CBL_SelectCustomer.DataBindings.Clear();
like image 22
Jacob Seleznev Avatar answered Sep 21 '22 12:09

Jacob Seleznev