在 VB.Net 中的标签上显示数据库记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22437348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:06:35  来源:igfitidea点击:

Displaying database record on a label in VB.Net

mysqlvb.net

提问by Sky Scraper

I have this problem that when I put id no. in the textbox1 and click the button1 the record successfully shows, but when I change the id no. in the textbox1 and click button1 again the error says

我有这个问题,当我把 id no. 在 textbox1 中并单击 button1 记录成功显示,但是当我更改 id 时。在 textbox1 中再次单击 button1 错误说

ArgumentException was unhandled

This causes two bindings in the collection to bind to the same property. Parameter name: binding

ArgumentException 未处理

这会导致集合中的两个绑定绑定到同一个属性。参数名称:绑定

I really don't understand the meaning of this, by the way still new and getting used to vb.net

我真的不明白这是什么意思,顺便说一下还是新的并且习惯了 vb.net

Imports MySql.Data.MySqlClient

Public Class Form16

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim thisConnection As New MySqlConnection("server=localhost;user id=root;database=db")
        Dim DataSet1 As New DataSet


        Dim sql As String = "SELECT * FROM voter where vid='" & TextBox1.Text & "'"
        Dim da As New MySqlDataAdapter(sql, thisConnection)

        da.Fill(DataSet1, "db")
        Label2.DataBindings.Add("text", DataSet1, "db.fname")
        Label10.DataBindings.Add("text", DataSet1, "db.mi")
        Label11.DataBindings.Add("text", DataSet1, "db.lname")
        Label12.DataBindings.Add("text", DataSet1, "db.yr")
        Label13.DataBindings.Add("text", DataSet1, "db.sec")
        Label14.DataBindings.Add("text", DataSet1, "db.vstatus")

    End Sub
End Class

采纳答案by Bj?rn-Roger Kringsj?

If you don't clearthe DataBindingscollection beforerebinding, this error will be thrown because a control can only have onebinding to a specified property.

如果不清除DataBindings收集之前重新绑定,这种错误将被抛出,因为控制只能有一个绑定到一个指定的属性。

For instance, Label2can only have one binding to the property Text. But when you're clicking the button for the second time the binding added in the first click is still in the collection.

例如,Label2只能有一个绑定到属性Text。但是当您第二次单击按钮时,第一次单击时添加的绑定仍在集合中。

First click:

首先点击:

Label2.DataBindings.Add("Text", DataSet1, "db.fname")

Second click:

第二次点击:

'Will throw an error because there's already a binding to property `Text`:
Label2.DataBindings.Add("Text", DataSet1, "db.fname")

Before you add a new binding, be sure the binding doesn't already exists.

在添加新绑定之前,请确保该绑定尚不存在。

'Always do a reversed loop when removing items from a collection.
For index As Integer = (Label2.DataBindings.Count - 1) To 0 Step -1
    If (Label2.DataBindings.Item(index).PropertyName = "Text") Then
        'Ops, a binding to the property `Text` already exists.
        'Remove this and we'll be fine.
        Label2.DataBindings.RemoveAt(index)
    End If
Next

'We can now safely add a new binding to the `Text` property.
Label2.DataBindings.Add("Text", DataSet1, "db.fname")

A more simpler way is to just clear the collection.

更简单的方法是清除集合。

Label2.DataBindings.Clear()
Label2.DataBindings.Add("Text", DataSet1, "db.fname")