Click here to Skip to main content
15,938,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i create project to search records by name.at 1st time i can type name and get infomation to textboxes and then im type another name and try to seaech records. in this time i got erro msg. its there "causes two bindings in the collection to bind same property.
parameter name: binding"
this is my code
Sub bind()

TextBox1.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".No")
DateTimePicker1.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Date")
TextBox2.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Amount")
TextBox3.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Cheque")
TextBox4.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Cash")
TextBox5.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Balance")
TextBox6.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".AC_No")
TextBox7.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Cheuq_no")
DateTimePicker2.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Released_date")
TextBox8.DataBindings.Add("text", ds, "" & ComboBox1.Text & ".Value")

End Sub

Sub binds()
Label13.DataBindings.Add("Text", ds, "customers.C_name")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ds.Clear()
da = New SqlDataAdapter("SELECT * FROM customers WHERE C_code = '" & ComboBox1.Text & "'", DBConn)
' SqlComm.CommandText = "SELECT * FROM Personal_Information where " + cbSearch.Text + " LIKE '%" & txtSearch.Text & "%'" = searcher
Try
da.Fill(ds, "customers")
binds()
Catch ex As Exception
' MessageBox.Show(ex.Message)
Exit Sub
DBConn.Close()
ds.Clear()
End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

ds.Clear()
da = New SqlDataAdapter("SELECT * FROM " & ComboBox1.Text & " WHERE No= " & TextBox9.Text & "", DBConn)
Try
da.Fill(ds, "" & ComboBox1.Text & "")
bind()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
DBConn.Close()

End Try
End Sub
whats wrong ??? any one can help me. thanks
Posted
Updated 12-Feb-12 14:35pm
v2

1 solution

You are probably binding AGAIN instead of repopulating your binding source.
Consider the following:
VB
Private Sub RefreshBindings
   ' Hitting this code twice will throw an Exception!
   TextBox.DataBindings.Add("Text", myBindingSource, "TextProperty")
End Sub

Private Sub LoadForm Handles Me.Load
   myBindingSource.DataSource = New MyBindingObject() ' Whatever it is.
   RefreshBindings()
End sub

Private Sub btnSave_Click Handles btnSave.Click
   Save(myBindingSource) ' Whatever this is.
   RefreshBindings()
End Sub
That exact code will throw this error because when you save your changes to the datasource you are 'refreshing' your bindings. No, what actually happens is that you set another binding on your TextBox. In this case the binding already exists and an Exception is thrown.
Consider the following code, which WILL run.
VB
Private Sub RefreshBindings
   myBindingSource.DataSource = New MyBindingObject() ' Whatever it is.
End Sub

Private Sub LoadForm Handles Me.Load
   TextBox.DataBindings.Add("Text", myBindingSource, "TextProperty")
   RefreshBindings()
End sub

Private Sub btnSave_Click Handles btnSave.Click
   Save(myBindingSource) ' Whatever this is.
   RefreshBindings()
End Sub
In this code the binding is only assigned once to the TextBox, but the DataSource of myBindingSource is refreshed. In other words, your binding stays the same, while your datasource is refreshed or repopulated.
Hope that helps! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900