为什么我得到"行/列没有数据"我有数据时出错?

时间:2014-12-31 14:57:06

标签: database vb.net ms-access datagridview textbox

要发生的事情是,当在数据库上双击一行时,程序应该从该行中提取数据并将其填入文本框中。这是我到目前为止的代码,双击我得到的行"行/列没有数据"。

谢谢,

Private Sub dtg_Email_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtg_Email.DoubleClick
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim dr As OleDbDataReader

    Try
      conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandText = CommandType.Text
        ReadPlayerID = Team_Database.PlayerID
        cmd.CommandText = "SELECT Surname, Forename, Email FROM PlayerDatabase WHERE ID = " & ReadPlayerID
        dr = cmd.ExecuteReader


        Me.txt_Surname.Text = IIf(Not IsDBNull(dr("Surname")), dr("Surname"), "")
        Me.txt_Forename.Text = IIf(Not IsDBNull(dr("Forename")), dr("Forename"), "")
        Me.txt_Email.Text = IIf(Not IsDBNull(dr("Email")), dr("Email"), "")

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

enter image description here

3 个答案:

答案 0 :(得分:2)

您需要调用dr.Read前进到下一个(本例中为第一个)记录,然后才能从第一行获取任何数据。

dr = cmd.ExecuteReader

dr.Read

Me.txt_Surname.Text = IIf(Not IsDBNull(dr("Surname")), dr("Surname"), "")

答案 1 :(得分:0)

至少对于其他DataReader来说," ExecuteReader"仅初始化读者,而您必须使用dr.Read迭代结果集一次。也许这对你的案子来说是真实的。

答案 2 :(得分:0)

上设一个断点

Me.txt_Surname.Text = IIf(Not IsDBNull(dr(" Surname")),dr(" Surname"),"")

一旦它击中破坏鼠标在博士上并右击快速监视。

检查quckwatch是否填充了博士并且有数据。