rrhandle8
asked on
session Datatable in class
I got a datatable, and them put it into a session variable.
Now I want to loop through it in a class.
See code attached
I get the error below on line 12.
Exception Details: System.NullReferenceExcept ion: Object variable or With block variable not set.
Source Error:
Line 10: Public Shared Function Privilege_Valid(ByVal strPage As String, ByVal strControl As String) As Boolean
Line 11: Dim r As DataRow
Line 12: For Each r In System.Web.HttpContext.Cur rent.Sessi on("Privil eges").row s
Line 13: Debug.Print(r.Item("Privil ege_ID"))
Line 14: Next
Now I want to loop through it in a class.
See code attached
I get the error below on line 12.
Exception Details: System.NullReferenceExcept
Source Error:
Line 10: Public Shared Function Privilege_Valid(ByVal strPage As String, ByVal strControl As String) As Boolean
Line 11: Dim r As DataRow
Line 12: For Each r In System.Web.HttpContext.Cur
Line 13: Debug.Print(r.Item("Privil
Line 14: Next
Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Web.SessionState
Imports System.Data
Imports System.Diagnostics
Public Class Privileges
Public Shared Function Privilege_Valid(ByVal strPage As String, ByVal strControl As String) As Boolean
Dim r As DataRow
For Each r In System.Web.HttpContext.Current.Session("Privileges").rows
Debug.Print(r.Item("Privilege_ID"))
Next
End Function
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yip, but you should always check that its still in the session. I'm not suggesting that the session is nothing, I'm saying check that the object you're extracting from session is not nothing. Thats just standard coding practice.
Then once your code works rather than throwing an error, and if you find its not in the session, then you need to investigate why not. For some reason its not the same session. Where does the above code live? Where is it called from? Where do you add the datatable to session? Is it definitely a datatable you're adding to the session? You're not accidentally adding a dataset for example?
I'd add some checks in the page directly after adding it to make sure its really there, then on another page, and on the page where you call this function and track it down to find out where it is going wrong.
What are you session settings in web.config?
Then once your code works rather than throwing an error, and if you find its not in the session, then you need to investigate why not. For some reason its not the same session. Where does the above code live? Where is it called from? Where do you add the datatable to session? Is it definitely a datatable you're adding to the session? You're not accidentally adding a dataset for example?
I'd add some checks in the page directly after adding it to make sure its really there, then on another page, and on the page where you call this function and track it down to find out where it is going wrong.
What are you session settings in web.config?
ASKER
Well, it is always Nothing the first time through, but then it always runs twice. The second time through it works.
Thanks, here is the code in VB that works
Imports System.Web.SessionState
Imports System.Data
Imports System.Diagnostics
Public Class Privileges
Public Shared Function Privilege_Valid(ByVal strPage As String, ByVal strControl As Control) As Boolean
Dim r As DataRow
Dim dt As New DataTable
dt = System.Web.HttpContext.Cur rent.Sessi on("Privil eges")
If Not (dt Is Nothing) Then
For Each r In dt.Rows
Debug.Print(r.Item(1))
Next
End If
End Function
Thanks, here is the code in VB that works
Imports System.Web.SessionState
Imports System.Data
Imports System.Diagnostics
Public Class Privileges
Public Shared Function Privilege_Valid(ByVal strPage As String, ByVal strControl As Control) As Boolean
Dim r As DataRow
Dim dt As New DataTable
dt = System.Web.HttpContext.Cur
If Not (dt Is Nothing) Then
For Each r In dt.Rows
Debug.Print(r.Item(1))
Next
End If
End Function
ASKER
The whole sense of putting it in a session is so I don't have to connect to the database every time a new page is requested.