8

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?

Srihari
  • 2,387
  • 9
  • 51
  • 82

3 Answers3

14

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");
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
5

Try this before binding:

CBL_SelectCustomer.DataBindings.Clear();
Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
1

I know this is an old post but in my case I kept getting that error because I had been calling a form_load event in de forms constructor (under InitializeComponent();, a mistake when copy pasting). That kept loading the form twice each time which isn't noticeable till you encounter funky behavior like this. If that's your case, deleting that this.Load += new System.EventHandler(Form1_Load) (or similar call) will help.

Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47