By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

Null Reference Exception When Using Sesssion State

P: n/a
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.
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Do you have EnableSessionState="false" in either the @ Page directive
or in the web.config, perhaps?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 14 Feb 2005 13:11:06 -0800, "Matt"
<Ma**@discussions.microsoft.com> wrote:
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.


Nov 19 '05 #2

P: n/a
Do you have EnableSessionState="false" in either the @ Page directive
or in the web.config, perhaps?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 14 Feb 2005 13:11:06 -0800, "Matt"
<Ma**@discussions.microsoft.com> wrote:
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.


Nov 19 '05 #3

P: n/a
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!

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 14 Feb 2005 13:11:06 -0800, "Matt"
<Ma**@discussions.microsoft.com> wrote:
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.


Nov 19 '05 #4

P: n/a
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!

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 14 Feb 2005 13:11:06 -0800, "Matt"
<Ma**@discussions.microsoft.com> wrote:
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.


Nov 19 '05 #5

P: n/a
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

"Matt" wrote:
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.

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.