【问题标题】:Data binding to Textboxes数据绑定到文本框
【发布时间】:2014-09-09 15:10:02
【问题描述】:
   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")

我不知道为什么它由于某种原因不能工作它没有填充数据集我是不是声明了一些错误?

【问题讨论】:

  • 您的编辑使当前接受的答案看起来不正确,因为它引用了一个不再存在的错误。由于您没有评论您接受that answer 的原因,因此回滚您的编辑似乎比更新答案更好。

标签: sql sql-server vb.net


【解决方案1】:

它不起作用的原因是因为你指定传入的表应该映射到一个名为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

【讨论】:

  • 对不起,我已经命名了表设置。我的老板来得很早,给我改了名字,仍然在其他地方根除错误:(。会看看那个。
  • 改变了周围仍然没有得到任何东西。直接通过 ssms 针对服务器的 sql 语句可以很好地拉起我想要的行,只是它似乎没有绑定。
  • 非常感谢,我现在还有很多阅读要做,以了解您所做的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多