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
 

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

Top