Solved

session Datatable in class

Posted on 2011-09-16
4
251 Views
Last Modified: 2012-05-12
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.NullReferenceException: 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.Current.Session("Privileges").rows
Line 13:             Debug.Print(r.Item("Privilege_ID"))
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

Open in new window

0
Comment
Question by:rrhandle8
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 250 total points
ID: 36552999
You always need to test that it exists and if not reload it. Sorry can't do VB but in C#:

DataTable Data = System.Web.HttpContext.Current.Session("Privileges") as DataTable;

if (Data != null)
{
  foreach(DataRow R in Data.Rows)
  {
    Debug.Print(R.Item("Privilege_ID"))
  }
}
else
{
  // Reload the data
}
0
 

Author Comment

by:rrhandle8
ID: 36553033
The session is not Nothing, but when I test the datatable, as in your code, it is always Nothing.
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.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 36553040
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?
0
 

Author Comment

by:rrhandle8
ID: 36553098
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.Current.Session("Privileges")
        If Not (dt Is Nothing) Then
            For Each r In dt.Rows
                Debug.Print(r.Item(1))
            Next
        End If
    End Function
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now