NullReferenceException未处理“对象引用未设置为对象的实例”。

时间:2012-05-19 05:44:33

标签: vb.net-2010

这是我在ojt的项目。我在ds(DataSet)中遇到问题,错误说它是null,现在,我的问题是,我该如何解决这个问题?

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

1 个答案:

答案 0 :(得分:0)

DataTable构造函数接收数据表的名称,这是可选的。您需要实例化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