[英]Invalid attempt to access a field before calling Read() with MySql
I am trying to run this code, but this error always pops up when I click on data grid view. 我正在尝试运行此代码,但是当我单击数据网格视图时,总是弹出此错误。
What do you think is the error here? 您认为这里的错误是什么?
Private Sub dgvRecords_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvRecords.CellClick
Dim intID As Integer
Try
Integer.TryParse(dgvRecords.Item(0, dgvRecords.CurrentRow.Index).Value, intID)
Dim dbQuery As String = "Select * from tblstudents where id = " & intID
Dim dbConnection As New MySqlConnection(dbConString)
Dim dbCmd As New MySqlCommand(dbQuery, dbConnection)
Dim dbReader As MySqlDataReader
dbConnection.Open()
dbReader = dbCmd.ExecuteReader()
txtStudID.Text = dbReader("id")
txtLastname.Text = dbReader("lastname")
txtFirstname.Text = dbReader("firstname")
txtMiddlename.Text = dbReader("middlename")
txtAddress.Text = dbReader("address")
cmbGender.Text = dbReader("gender")
txtContact.Text = dbReader("contact")
cmbCivilStatus.Text = dbReader("civil_status")
dbReader.Close()
dbConnection.Close()
Catch ex As Exception
MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
End Try
End Sub
You are getting that error cause you are not calling the Read()
method on reader object and without which reader object can't advance to the firs row in result set to start fetching. 您收到该错误的原因是,您没有在
Read()
器对象上调用Read()
方法,并且没有该读取器对象无法前进到结果集中的首行以开始提取。 That's what the error says exactly. 这就是错误确切说明的内容。 You should do like
你应该喜欢
While(dbReader.Read())
{
txtStudID.Text = dbReader("id")
txtLastname.Text = dbReader("lastname")
.......
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.