简体   繁体   English

使用MySql调用Read()之前尝试访问字段无效

[英]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.

相关问题 在调用Read()之前尝试访问字段无效 - Invalid attempt to access a field before calling Read() 调用read()之前访问字段的尝试无效 - Invalid attempt to access field before calling read() 在调用Read()之前尝试访问字段无效 - Invalid attempt to access a field before calling Read() 在调用Read()INSERT之前无法尝试访问字段 - Invalid attempt to access a field before calling Read() INSERT 调用Read()vb.NET和MySQL之前尝试访问字段无效 - Invalid attempt to access a field before calling Read() vb.NET and MySQL 在调用read()之前(也在调用read之后)无效的访问字段的尝试 - invalid attempt to access a field before calling read() (also after read is called) 在调用Read()之前获得无效的访问字段的尝试,但是在C#中调用了Read() - Get invalid attempt to access a field before calling Read() , but Read() is called in C# 在调用Read()之前尝试访问字段无效,但是我先调用Read() - Invalid attempt to access a field before calling Read(), but I call Read() first 尝试读取无效-是否可以使用Action? - Invalid attempt to read - Is possible using Action? 我试图从 microsft sql 读取数据然后插入到 Mysql 但我收到一条错误消息“没有数据时读取无效” - im trying to read data from microsft sql then insert to Mysql but im getting an error stating "Invalid attempt to read when no data is present"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM