将表中的数据绑定到文本框

问题描述:

我创建了一个按名称搜索记录的项目.第一次,我可以键入name并将信息发送到文本框,然后我键入另一个名称并尝试查找记录.在这个时候我得到了错误的味精.它的存在导致集合中的两个绑定绑定相同的属性.
参数名称:绑定"
这是我的代码
子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")

结束子

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

结束子

私有子Button1_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理Button1.Click
ds.Clear()
da =新的SqlDataAdapter(选择*来自客户的C_code =''"& ComboBox1.Text&",DBConn)
" SqlComm.CommandText ="SELECT * FROM Personal_Information其中" + cbSearch.Text +喜欢"%& txtSearch.Text& %""=搜索者
试试
da.Fill(ds,"customers")
binds()
异常捕获
''MessageBox.Show(ex.Message)
退出子
DBConn.Close()
ds.Clear()
结束尝试
结束子

私有子Button3_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理Button3.Click

ds.Clear()
da =新的SqlDataAdapter("SELECT * FROM"& ComboBox1.Text&"WHERE No ="& TextBox9.Text&,DBConn)
试试
da.Fill(ds,"& ComboBox1.Text&")
bind()
异常捕获
MessageBox.Show(ex.Message)
终于
DBConn.Close()

结束尝试
结束子
怎么了 ???任何人都可以帮助我.谢谢

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

您可能正在重新绑定而不是重新填充绑定源.
请考虑以下内容:
You are probably binding AGAIN instead of repopulating your binding source.
Consider the following:
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

确切的代码将引发此错误,因为将更改保存到数据源时您正在刷新"您的绑定.不,实际发生的是在TextBox上设置了另一个绑定.在这种情况下,绑定已经存在,并引发Exception.
考虑下面的代码,它将运行.

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.

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

在此代码中,绑定仅分配给TextBox一次,但将DataSource分配给TextBox myBindingSource的刷新.换句话说,在刷新或重新填充数据源时,绑定保持不变.
希望对您有所帮助! :)

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! :)