How to test for HttpContext.Current.Session variable

  • Thread starter Thread starter moondaddy
  • Start date Start date
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?
 
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...
 
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.
 
Back
Top