473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Null Reference Exception When Using Sesssion State

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
5 4466
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shannon | last post by:
I am having a very stange issue happen to my web application. I am trying to develop a simple shopping cart. The code works perfectly when I run the application under http://localhost/myapp. ...
6
by: vasu devan via DotNetMonster.com | last post by:
Hi, I am two config section with same config section handler like follow. <section name="Myformatters" type="MyFramework.Configuration.FileHandlers.MyConfigFileHandler, MyFramework"/> <section...
0
by: Matt | last post by:
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...
2
by: ankur seth via DotNetMonster.com | last post by:
Getting error Null Reference exception when a procedure is called in vb.net ..Why does it occur and how to resolve it. --- ANkur -- Message posted via http://www.dotnetmonster.com
1
by: A_StClaire_ | last post by:
hi, I have a singleton class called 'Evaluation' which is a utility class used to determine the strength of a poker hand. two members of 'Evaluation' are highCard and lowCard, two objects of...
0
by: webbsk | last post by:
I keep getting an exception when I call the DataAdapter Update method. I have been trying to figure out what is causing the null reference exception for this code for what seems like forever: ...
2
by: Simon Rigby | last post by:
Hi folks, A bizarre problem I am having. I have a treeview which is bound to an XmlDataSource. The XMLDataSource.Data property is set to the result of a function that generates an XML...
2
by: Wayne Brantley | last post by:
If you expose a public property of a usercontrol that accesses an object on the user control it will give a null reference exception when you wrap that in an update panel. Others are having the...
1
by: labby | last post by:
Hi, I've application to capture video of image, grab the still image and process it. It runs well until I put loop to regrab and the still images many times automatically.. and the error happened....
7
by: pinman | last post by:
hi i'm recieveing something called a null reference exception whenever i try to run my code. i am testing for a null value and if it is null i carry out some action, if false i recursively call the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.