什麼意思是,當數據庫上雙擊一行時,程序應該將數據從該行中拉出並填充到文本框中。這是我到目前爲止的代碼,當雙擊一行時,我得到「沒有數據存在的行/列」。當我有數據時,爲什麼會出現「沒有數據存在行/列」錯誤?
感謝,
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
關於您的代碼風格,您應該將連接封裝在Using塊中,然後您需要再次關閉閱讀器。通過'If(statement,trueValue,falseValue)'語法廢棄'IIf'。 – LWChris