数据绑定到Textboxes

时间:2014-09-09 15:10:02

标签: sql sql-server vb.net

   Dim myconn As New SqlConnection("Server=server,Trusted_Connection=True,Database=database")
    'selects from mt table linking the current pc to a row
    Dim sql As String = "SELECT * from idset " & vbcrlf &
    "Where pcname= '" & pcname & "'"
    Dim ds As New DataSet
    Dim da As New SqlDataAdapter(sql, myconn)

    da.Fill(ds, "Setup")


    txtClientID.DataBindings.Add("text", ds.Tables("idset"), "CLID")

我不知道为什么它因为某种原因不能正常工作而没有填写数据集我是否说错了?

1 个答案:

答案 0 :(得分:3)

它之所以不起作用,是因为您指定传入的表应该映射到名为Setup的表。您的数据集不包含名为Setup的表,因此传入的表将被命名为......好,Setup

请改为尝试:

da.Fill(ds, "idset")

此外,我 强烈 建议你:

严重。


调试绑定的最佳方法是使用BindingSource并处理BindingComplete事件。

Private bs As BindingSource
Private ds As DataSet

Private Sub Initialize(pcname As String)

    Me.ds = New DataSet()

    Using connection As New SqlConnection("Server=server,Trusted_Connection=True,Database=database")
        connection.Open()
        Using command As New SqlCommand()
            command.Connection = connection
            command.CommandText = "SELECT * from [idset] Where [pcname] = @pcname;"
            command.Parameters.AddWithValue("@pcname", pcname)
            Using adapter As New SqlDataAdapter(command)
                adapter.Fill(Me.ds, "idset")
            End Using
        End Using
    End Using

    Me.bs = New BindingSource(Me.ds, "idset")

    AddHandler bs.BindingComplete, AddressOf Me.HandleBindingCompleted

    Me.txtClientID.DataBindings.Add("Text", Me.bs, "CLID")

End Sub

Private Sub HandleBindingCompleted(sender As Object, e As BindingCompleteEventArgs)
    If (Not e.Exception Is Nothing) Then
        Debug.WriteLine(e.ErrorText)
    End If
End Sub