Why do I get “No data exists for the row/column” error when I do have data?

ぃ、小莉子 提交于 2019-12-13 04:44:15

问题


What's meant to happen is that when the a row is double clicked on the database the program should pull the data out of that row and fill it in the text boxes. This is the code I have so far, when double clicking a row I get "No data exists for the row/column".

Thanks,

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


回答1:


You need to call dr.Read to advance to the next (first in this case) record before you can grab any data from the first row.

dr = cmd.ExecuteReader

dr.Read

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



回答2:


At least for other DataReaders it is common that "ExecuteReader" only initializes the reader, while you have to iterate over the result set with dr.Read once. Maybe that's true for your case.




回答3:


Put a break point on

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

Once it hits the break mouse over the dr and right click on quick watch.

check in the quckwatch if the dr is filled and has data.



来源:https://stackoverflow.com/questions/27722734/why-do-i-get-no-data-exists-for-the-row-column-error-when-i-do-have-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!