Reputation: 55
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?
Upvotes: 1
Views: 296
Reputation: 216313
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
Upvotes: 2