I’m having problem regarding session items. Before I use them, I want to check if they exists, but using this codes gives me error:
If (Session("SomeSessionItem") Is Nothing) Then
...
End If
This is the error:
Object reference not set to an instance of an object.
I think Session("SomeSessionItem") tries to acquire the value of the session item. If the item doesn’t exists then it throws exception. But how do I check if a session item exists before using them?
- I have a page
Home.aspx. - In the
Home.aspx.vb, I instantiate a WebUserControlSomeControl.ascx. Note that inHome.aspx.vbevent handlerPage_LoadI can use a condition to check session without getting an exception. - Inside
SomeControl.ascx.vbI’m trying to access the session, here’s where the exception occurs.
Does that work for you?
Also, you may need to check
HttpContext.Current.Sessionrather than simplySessionif you’re seeing the the following error: