简体   繁体   中英

There is no row at position 0 Vb.net and Mysql

I'm trying to get and print data from a row "status" on the table "printer" and it keeps saying "There is no row at position 0."

 Dim conn As New deepconnection()
    Dim adapter As New MySqlDataAdapter()
    Dim table As New DataTable()
    Dim ds, ds1 As New DataSet
    Dim command As New MySqlCommand("SELECT * FROM printer", conn.getConnection)
    conn.openOcean()
    PrinterStatus.Text = table.Rows(0).Item("status")

The connection:

 Private fishcatch As New MySqlConnection("datasource=localhost;port=3306;username=root;password=xxxxx;database=deep_ocean")

' Get the connection only to read
ReadOnly Property getConnection() As MySqlConnection
    Get
        Return fishcatch
    End Get
End Property

Open connection:

Sub openOcean()
    If fishcatch.State = ConnectionState.Closed Then
        fishcatch.Open()
    End If
End Sub

What is wrong?

You haven't executed the command to fill the table. Without that part the table is still empty

Dim command As New MySqlCommand("SELECT * FROM printer", conn.getConnection)
conn.openOcean()

' Execute the command and pass the reader to the table load method
table.Load(command.ExecuteReader())

PrinterStatus.Text = table.Rows(0).Item("status")

Even after this the table could still be empty if there is no record in the database table named Printer, so before reading anything from a datatable check always the rows count

If table.Rows.Count > 0 Then
    PrinterStatus.Text = table.Rows(0).Item("status")
    ...
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM