Null Reference Exception When Using Sesssion State

G

Guest

Hi all,

I'm having a very strange problem. The third line in the following code
gives me a null reference exception when used in an ASP.Net web page. The
code is called on the click of a button.

Public Overridable Property SDSVarsSetup() As Boolean
Get
If Not IsNothing(Session("SDSVarsSetup")) Then ' <-- **Exception
occurs here**
Return CType(Session("SDSVarsSetup"), Boolean)
Else
Return Nothing
End If
End Get
Set(ByVal Value As Boolean)
If Not IsNothing(HttpContext.Current) Then
Session("SDSVarsSetup") = Value
End If
End Set
End Property

I don't understand how a null reference exception can occur when I'm
checking for a null reference condition. I have checked the code at a
different stage of the page life cycle (i.e. Load) and the values are present
and work fine. It appears to be a problem with the Handle Postback Events
stage. Does anyone know why this might happen?

Thank you very much.
 
S

Scott Allen

Do you have EnableSessionState="false" in either the @ Page directive
or in the web.config, perhaps?
 
G

Guest

Scott,

Thanks for your idea.

I thought of that, but the session information is available at other stages
in the page lifecycle, so Session is working just not at the Handle Postback
events stage. But to explicitly answer your question, yes, sessionstate is
enabled via the webconfig, and I don't explicity set it anywhere else.

Is there a way in code to check to see if session is enabled?

Also something to note, I have the trace on and can see the session variable
that I'm looking for in the trace output. Hope someone can help. I need to
get this fixed ASAP.

Thank you very much!
 
G

Guest

Ok, for some reason (unclear to me) when I changed the code to use
System.Web.HttpContext.Current.Session("VarName")
instead of
Session("VarName")
it fixed all of the problems. Can anyone explain this to me? I would
appreciate it.

Thanks,
Matt
 
Joined
Jan 27, 2009
Messages
1
Reaction score
0
Null Reference Exception When Using Session State

I found this today which may be the reasoning you were looking for:

msdn.microsoft.com/en-us/library/system.web.httpcontext.session(VS.80).aspx

Specifically in the "Remarks" section it states

The Session property provides programmatic access to the properties and methods of the HttpSessionState class. Because ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. For example, you can use just Session("SessionVariable1") to get or set the value of the session state variable SessionVariable1. However, if you want to use the members of HttpResponse from an ASP.NET code-behind module, you must include a reference to the System.Web namespace in the module and also fully qualify the reference to both the currently active request/response context and the class in System.Web you want to use. For example, in a code-behind page you must specify the full name HttpContext.Current.Session("SessionVariable1").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top