Results 1 to 13 of 13

Thread: Two bindings in the collection to bind to the same property

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    14

    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)

  2. #2
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    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?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    14

    Re: Two bindings in the collection to bind to the same property

    Quote Originally Posted by Tewl View Post
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    14

    Re: Two bindings in the collection to bind to the same property

    Quote Originally Posted by jmcilhinney View Post
    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Two bindings in the collection to bind to the same property

    Quote Originally Posted by billybobcat View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    14

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    14

    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 !

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    New Member
    Join Date
    Mar 2011
    Posts
    1

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

  12. #12
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Two bindings in the collection to bind to the same property

    Welcome to the forum, Abid.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Re: Two bindings in the collection to bind to the same property

    Quote Originally Posted by abidmix View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width