Another Session variable question

mpdillon

Member
Joined
Feb 17, 2005
Messages
20
Location
Glen Mills, PA
Programming Experience
Beginner
If I test against a Session variable before I assign a value to it I get the following error.
"Object reference not set to an instance of an object"
How do I determine if the Session variable exists before I test it?
(I read elsewhere that you could assign the contents of a Session variable to another varible and this would yield a value of "". I tried this but still got the same error message.)
thanks

Context: I want to store the choice of radio buttons on a page. When the user navigates back to this page I want to use the session variable to set the choice they had made previously. Works fine except for the first time they load the page.
 

mpdillon

Member
Joined
Feb 17, 2005
Messages
20
Location
Glen Mills, PA
Programming Experience
Beginner
Solution

To test if a session variable has been set, use:

If Session("VariableName") is nothing then
'VariableName was never created
else
'VariableName was created and it is OK to test it for content
If len(Session("VariableName").ToString)> 0 then
'Do Something
else
'Do Something else
end if
end if

This should have been easy for me but wasn't. Before the original post I couldn't find anything about the error message when I searched online. Today I tried again and found it.
 
Top Bottom