saving objects to application throwing null reference error

I am having a problem and I don't know why. I am trying to save a datatable to application state and I cant seem to be able to acheive this.

Here is what i'm doing:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

Dim sqlConn As New SqlConnection(PubVars.connString)
Dim sqlCmd As New SqlCommand("SELECT * from tbl1", sqlConn)
Dim allMembersDset As New DataSet
Dim allMembersDA As New SqlDataAdapter(sqlCmd)
allMembersDA.Fill(allMembersDset, "tbl1")
sqlConn.Close()
Application("allMembers") = allMembersDset.Tables("tbl1")

End Sub

When I try to retrieve this table from another page like this:

Dim dt As New DataTable
dt = CType(Application.Item("allMembers"), DataTable)

I get the following error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

However if i save the datatable on session_start i dont get this problem.

I also get the same error when trying to save a table to application state inside a page:

Application("dTable")=myTable

Please help




Answer this question

saving objects to application throwing null reference error

  • Ramraj_Velmurugan

    Try:

    Application.Add("allMembers", datatable)


    FOr this sort of thing I usually use the Cache object:

    Cache.Add(...)

  • Krutika

    Thank you, it worked

  • saving objects to application throwing null reference error