How to test for HttpContext.Current.Session variable

M

moondaddy

I need to set a variable to a session variable (if that's what you call it)
like this:

dim ds as dataset = HttpContext.Current.Session("CustDataSet")

But I get an exception if this variable in the current session hasn't been
set yet so I need to test it. I tried:

If HttpContext.Current.Session("CustDataSet") = nothing then
HttpContext.Current.Session("CustDataSet") = GetCustDataSet()
End If

But I get an exception on this also and the exception message is:

"Object reference not set to an instance of an object."

How can I test for this?
 
M

moondaddy

I figured it out. It seems lately that no matter how much trouble shooting
I do I can't find the answer myself until I post it in the user group and
then Wa La! I figure it out.

If anyone cares.... I had the page property (EnableSessionState="False")
where it should be True. If its set to false, then none of the session
state stuff on the server is going to work. du...
 
E

Eric Lawrence [MSFT]

Just so you know, it's most likely the Session object itself which is null.
The proper way to code this in C# would be

if (null != HttpContext.Current && null != HttpContext.Current.Session &&
null != HttpContext.Current.Session("CustDataSet")){
GetCustDataSet();
}


--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top