0

this is my project in my ojt. I have a problem in ds(DataSet), the error says that it is null, now, my question is, how can I fix this problem?

Private Sub RefreshData()
    If Not con.State = ConnectionState.Open Then
        con.Open()
    End If

    Dim dt As New DataTable("SELECT ID as [ID], " & _
                                         "fname as [NAME], lname " & _
                                     "FROM asdf ORDER by ID")
    If ds IsNot Nothing And ds.Tables("asdf") IsNot Nothing Then****this part is were i        get the error***
        da.Fill(dt, "asdf")
    End If
    con.Close()
    maxrows = ds.Tables("asdf").Rows.Count
    inc = -1
End Sub
Cœur
  • 32,421
  • 21
  • 173
  • 232
  • possible duplicate of ["NullReferenceExeption was Unhandled"](http://stackoverflow.com/questions/10647206/nullreferenceexeption-was-unhandled) – kv-prajapati May 19 '12 at 05:54
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ekad Jan 07 '19 at 12:35

1 Answers1

0

DataTable constructor receive the name of data table and which is optional. You need to instantiate the DataAdapter.

Private Sub RefreshData()
    Dim Sql="SELECT ID as [ID],fname as [NAME], lname FROM asdf ORDER by ID"
    Dim con as new OleDbConnection("your_connection_string_here")
    Dim da as new OleDbDataAdapter(Sql,con)
    Dim dt As New DataTable
    da.Fill(dt)
    ...
End Sub
kv-prajapati
  • 90,019
  • 18
  • 141
  • 178