Two bindings in the collection to bind to the same property
I have a button in a form that loads a datagrid and two text boxes. When the form first opens, my datagrid and text boxes are blank. Clicking the button will load the datagrid and fill the two text boxes.
But clicking the button a second time will generate a "This causes two bindings in the collection to bind to the same property." error.
The error occurs at this line:
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
What codes should I add to the program to prevent the error?
The source code is as follows:
Code:
public partial class Form1 : Form
{
DataSet ds = new DataSet();
SqlConnection cs = new SqlConnection(StrConn);
SqlDataAdapter da = new SqlDataAdapter();
BindingSource tblNamesBS = new BindingSource();
private void btnDisplay_Click(object sender, EventArgs e)
{
da.SelectCommand = new SqlCommand("SELECT EmployeeId, FirstName, LastName FROM EMPLOYEE", cs);
ds.Clear();
da.Fill(ds);
dgGrid.DataSource = ds.Tables[0];
tblNamesBS.DataSource = ds.Tables[0];
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName")); // error here
txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "LastName"));
}
The error message as follows:
Code:
System.ArgumentException was unhandled
Message="This causes two bindings in the collection to bind to the same property.\r\nParameter name: binding"
Source="System.Windows.Forms"
ParamName="binding"
StackTrace:
at System.Windows.Forms.ControlBindingsCollection.CheckDuplicates(Binding binding)
at System.Windows.Forms.Binding.CheckBinding()
at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
Re: Two bindings in the collection to bind to the same property
What are you wanting to happen the second time you press the button?
Re: Two bindings in the collection to bind to the same property
If there's already a bound DataTable then why not just repopulate that? Otherwise, first check whether there's an existing binding and remove it if there is.
Re: Two bindings in the collection to bind to the same property
Quote:
Originally Posted by
Tewl
What are you wanting to happen the second time you press the button?
Not that I want that to happen.
I'm just doing this tutorial.
The codes in the tutorial is not good, clicking the buton second time will cause the error.
Re: Two bindings in the collection to bind to the same property
Quote:
Originally Posted by
jmcilhinney
If there's already a bound DataTable then why not just repopulate that? Otherwise, first check whether there's an existing binding and remove it if there is.
The sample codes in the tutorial is bad. That is why I what codes should I add to the program to prevent the error?
Ok, first check whether there's an existing binding and remove it if there is. How do you code this?
What code to use to check if there is existing binding?
What code to use to remove the binding?
Re: Two bindings in the collection to bind to the same property
Quote:
Originally Posted by
billybobcat
Ok, first check whether there's an existing binding and remove it if there is. How do you code this?
What code to use to check if there is existing binding?
What code to use to remove the binding?
A developer is supposed to use logic so use some logic. Look at the code you've already got:
Code:
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
That's the code to add a binding. That should be a rather big clue to the answers to your questions. If you call Add on DataBindings to add a binding, how do you suppose you remove a binding? As for finding out if there's an existing binding, that's obviously (I hope) going to have something to do with DataBindings too, so read the documentation for the DataBindings property and see what it's all about. Do what you can for yourself first, then ask others if you get stuck.
Re: Two bindings in the collection to bind to the same property
I'm very new to programming, and I haven't found the answer.
Re: Two bindings in the collection to bind to the same property
Then you haven't read the documentation for the DataBindings property, because that has the answer.
Re: Two bindings in the collection to bind to the same property
I will eat the humble pie and say my IQ is below average !
Re: Two bindings in the collection to bind to the same property
I don't expect everyone to know everything, especially beginners. What I do expect is that anyone who wants to be a developer is capable of opening documentation, reading and clicking a few links. If you have read the documentation for the Control.DataBindings property and followed the relevant links then you know what type it is, i.e. ControlBindingsCollection, and what members that type has, e.g. Add and Remove methods and Item property. If you know that information then you can have a go at using them. If your attempt fails then you can post what you know and what you tried here and then we can help you fix it. This is the stuff that you can always do for yourself no matter how much skill or experience you have. You can always try and we can help you if you fail, but you'll never succeed if you don;t try.
Re: Two bindings in the collection to bind to the same property
I agree to jmcilhinney on his advise on using logic.I was coding in c# and got that error and by checking the documentation I got hold of the clear property and it worked like a charm.
txtFirstBame.DataBindings.Clear();
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
txtLastname.DataBindings.Clear();
txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
Re: Two bindings in the collection to bind to the same property
Welcome to the forum, Abid.
Re: Two bindings in the collection to bind to the same property
Quote:
Originally Posted by
abidmix
I agree to jmcilhinney on his advise on using logic.I was coding in c# and got that error and by checking the documentation I got hold of the clear property and it worked like a charm.
txtFirstBame.DataBindings.Clear();
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
txtLastname.DataBindings.Clear();
txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
Thanks a lot for wise solution