This causes two bindings in the collection to bind to the same property.

Joined
Mar 3, 2012
Messages
1
Location
Cavite City, Philippines
Programming Experience
Beginner
hi.. im having difficulty in adding a new data on my mysql database.. everytime a add a student info the message "This causes two bindings in the collection to bind to the same property. Parameter name: binding" appears.. im new to vb.net hope u can help.. thanks in advance.. here's my code.. pls help..

VB.NET:
Public Sub connect()
        Dim MyCon As New MySqlConnection("server= localhost; user id= root; password= root; database= db")
        Dim SQL As String = "SELECT * FROM college"
        Dim MyCmd As New MySqlCommand(SQL, MyCon)
        Dim MyDa As New MySqlDataAdapter(MyCmd)
        Dim MyDT As New System.Data.DataTable
        MyDa.Fill(MyDT)


        Try
            MyCon.Open()


            Try






[B]                txtnum.DataBindings.Add("Text", MyDT, "studnum")[/B]
[B]                txtname.DataBindings.Add("Text", MyDT, "studname")[/B]
[B]                txtgcn.DataBindings.Add("Text", MyDT, "gcn")[/B]
[B]                txtguard.DataBindings.Add("Text", MyDT, "guardian")[/B]
[B]                txtscn.DataBindings.Add("Text", MyDT, "scn")[/B]
[B]                cmbcourse.DataBindings.Add("Text", MyDT, "courses")[/B]
[B]                cmbgender.DataBindings.Add("Text", MyDT, "gender")[/B]
[B]                cmbgroup.DataBindings.Add("Text", MyDT, "groups")[/B]
[B]                cmbrel.DataBindings.Add("Text", MyDT, "relationship")[/B]
[B]                cmbdept.DataBindings.Add("Text", MyDT, "department")

* the error is in here*[/B]


                dgv.DataSource = MyDT


                manage = DirectCast(Me.BindingContext(MyDT), CurrencyManager)


            Catch myerror As MySqlException


                MsgBox("There was an error reading from the database: " & myerror.Message)


            End Try
        Catch myerror As MySqlException


            MessageBox.Show("Error connecting to the database: " & myerror.Message)


        Finally


            If MyCon.State <> ConnectionState.Closed Then MyCon.Close()


        End Try


        cmdsave.Enabled = True
        txtTotal.Text = manage.Count
        MyDa.Dispose()
        MyCmd.Dispose()


    End Sub

and my code in add
VB.NET:
Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click        Dim MyCon As New MySqlConnection("server= localhost; user id= root; password= root; database= db")
        Dim MyCmd As New MySqlCommand(0, MyCon)


        If cmdsave.Text = "Save" Then


            MyCmd.CommandText = "INSERT INTO college(groups, department, studnum, studname, gender, courses, scn, guardian, relationship, gcn)" _
            & "VALUES(?groups, ?department, ?studnum, ?studname, ?gender, ?courses, ?scn, ?guardian, ?relationship, ?gcn)"
            MsgBox("Item Added!!", MsgBoxStyle.Information)


        Else


            MyCmd.CommandText = "update college set groups=?groups, department=?department, studname=?studname, gender=?gender, courses=?courses, scn=?scn, guardian=?guardian, relationship=?relationship, gcn=?gcn where studnum=" & txtnum.Text & ""
            MsgBox("Item Updated!!", MsgBoxStyle.Information)
            cmdsave.Text = "Save"


        End If


        Try


            MyCmd.Parameters.AddWithValue("?studnum", txtnum.Text)
            MyCmd.Parameters.AddWithValue("?studname", txtname.Text)
            MyCmd.Parameters.AddWithValue("?gcn", txtgcn.Text)
            MyCmd.Parameters.AddWithValue("?scn", txtscn.Text)
            MyCmd.Parameters.AddWithValue("?guardian", txtguard.Text)
            MyCmd.Parameters.AddWithValue("?courses", cmbcourse.Text)
            MyCmd.Parameters.AddWithValue("?department", cmbdept.Text)
            MyCmd.Parameters.AddWithValue("?gender", cmbgender.Text)
            MyCmd.Parameters.AddWithValue("?groups", cmbgroup.Text)
            MyCmd.Parameters.AddWithValue("?relationship", cmbrel.Text)
            MyCon.Open()
            MyCmd.ExecuteNonQuery()
            MyCon.Close()


        Catch myerror As MySqlException


            MsgBox("There was an error in the database: " & myerror.Message)


        End Try


        Call connect()


    End Sub
thanks..
 
Please don't create multiple threads for the same question. One thread per topic and one topic per thread please.

The issue is that you are binding the same properties of your controls multiple times. If your result set has the same schema every time then there's no need to create a new DataTable every time. Just bind to the one DataTable and then clear and refill it each time. Your controls remain bound to the same data source the whole time and you just change the data in the source. If you really do need to rebind the controls multiple times then you must remove the old bindings first.
 
Back
Top