James Stafford
James Stafford

Reputation: 1054

mysql select statement not filling datatable

I have a MySQL object I am trying to select all data from and insert into a datatable however every time it gets to my first if statement regarding said datatable It gets a possible null value error

dbfile dbserver dbuser and dbpassw all are public variables set on on a module.

variable dimensions

Dim cnn As New MySqlConnection()
Dim cmd As New MySqlCommand
Dim adptr As New MySqlDataAdapter
Dim filltab As DataTable

code

        Dim cb As New MySqlConnectionStringBuilder
    cb.Database = dbfile
    cb.Server = dbserver
    cb.UserID = dbuser
    cb.Password = dbpassw
    Using cnn As New MySqlConnection(cb.ConnectionString)
        Using cmd As New MySqlCommand("SELECT * from gpstracking", cnn)
            Try
                cnn.Open()
                adptr = New MySqlDataAdapter(cmd)
                adptr.Fill(filltab)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            If filltab.Rows.Count > 0 Then 'this is the row that the error for filltab being a null value hits
                pop = 0

                For pop As Integer = 0 To filltab.Rows.Count - 1
           '         WebBrowser1.Document.InvokeScript("AddMarker", New Object() {"Unit: " & filltab.Rows(pop)("unitnumb") & " at " & filltab.Rows(pop)("time"), filltab.Rows(pop)("lat"), table.Rows(pop)("long")})
                Next
            End If
        End Using
    End Using

one error on

adptr.Fill(filltab)  'Exception:Thrown: "Value cannot be null." (System.ArgumentNullException)

I also just noticed it is announcing "failure to collect details" on

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

Upvotes: 0

Views: 746

Answers (2)

Ramgy Borja
Ramgy Borja

Reputation: 2458

instead of using this code

adptr = New MySqlDataAdapter(cmd)
adptr.Fill(filltab)

try use

filltab.load(cmd.ExecuteReader())

Upvotes: 0

Max Yakimets
Max Yakimets

Reputation: 1225

I don't know vb syntax, but you have Dim filltab As DataTable without New (like in other dimensions), and I guess MySqlDataAdapter.Fill requires the argument to be not null. Just a guess.

Upvotes: 2

Related Questions