Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session ends when accessing an object stored in a session variable

Status
Not open for further replies.

ApocY85

Programmer
Dec 20, 2007
36
US
Hello everyone!

I have an issue I am hoping someone can help with. I have an application with quite a lot of code but managed to reduce all the code/references/etc down to just a few lines.

The issue that I am having is when I am accessing an object that was stored in a session variable, the session will end. Consequently, the session variable is cleared and I receive an "Object variable or with block variable not set" error.

More bizarre, it appears to only happen or happen more frequently when I run the application immediately after saving any changes. If I change the session to contain a standard data type instead, no error is encountered. It also only appears to happen across pages.

If anyone could help me figure out what is causing this issue or can provide an alternative solution, I would greatly appreciate it. Thank you in advance for your time and help!

To replicate my issue, please follow the steps below:

1) Create a new ASP.NET Empty Web Application using Visual Basic.NET.

2) Create two folders, "Modules" and "Pages".

3) Add a new, empty module, "Module 1.vb" to the "Modules" folder.

4) Add the following code to "Module 1":

Public Class testClass
Public testField As String
End Class

5) Add two new, empty web forms to the "Pages" folder, named "WebForm1.aspx" and "WebForm2.aspx".

6) In the code window for "WebForm1.aspx", add the following code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

''''' Object In Session Variable '''''
Dim test1 As New testClass
test1.testField = "test1"
Session("test1") = test1
''''''''''''''''''''''''''''''''''''''


''''' String In Session Variable '''''
'Session("test1") = "test1"
''''''''''''''''''''''''''''''''''''''


Response.Redirect("WebForm2.aspx")

End Sub

7) In the code window for "WebForm2.aspx", add the following code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

''''' Object In Session Variable '''''
Select Case Session("test1").testField
Case "test1"
End Select
''''''''''''''''''''''''''''''''''''''


''''' String In Session Variable '''''
'Select Case Session("test1")
'Case "test1"
'End Select
''''''''''''''''''''''''''''''''''''''

End Sub

8) Finally, the following is all that is needed in the "Web.config" file:

<?xml version="1.0"?>

<configuration>

<system.web>
<sessionState mode="InProc" timeout="60" />
<compilation debug="true" targetFramework="4.0" />
</system.web>

</configuration>

Make sure to use "WebForm1.aspx" as the startup page. If you don't obtain an error, type a letter into a code window, delete the letter, then click "Save All". You will notice that when you comment out the existing session code (the two portions labeled "Object In Session Variable") and uncomment the two portions labeled "String In Session Variable", the issue stops occurring. Also, it doesn't appear to matter whether the project uses .NET Framework 4.0 or 4.5, or if the application is run using Visual Studio Development Server or IIS Express.

Once again, thank you for any help!
 

I tried you example but was not able to duplicate your error. However, I don't recommend accessing the class objects directly from the Session like your example shows Session("test1").testField

You should always test to ensure the object is in session before accessing the properties of the object for the reason that you're experiencing now:

Code:
Dim objTest As testClass = Session("test1")

If objTest IsNot Nothing Then
      ''''' Object In Session Variable '''''
      Select Case objTest.testField
          Case "test1"
      End Select
Else
      ' Session object not found
End If

But you can also try adding the Serializable attribute to to your module

Code:
<Serializable()> _
Module Module1



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Marks comments are right on.
In addition to that, I would also do a DirectCast, or better yet a TryCast on the object that is in session(on page 2), when trying to access the object's property. This will help you determine if the object is somehow not being stored in session correctly.
 
MarkSweetland/jbenson001,

Thank you for your prompt responses! Unfortunately, I am still encountering the issue even though I tried adding <Serializable()>, as suggested above. Although adding the "IsNot Nothing" check should indeed be included in a real application, it does not resolve the issue at hand and was left out to keep the example simple. However, I do appreciate the suggestions.

The application will still not run correctly each time I make a change (which, aside from the annoyance, slows down development).

I noticed the problem only occurs when editing a code-behind file, but not a markup file. It still appears to occur only once each time after saving the application. Although other developers were also able to reproduce the issue, MarkSweetland (from above) and one developer in my building were unable to. The error also does not occur when the full application is run on our development and production servers (as opposed to on a local PC). These facts lead me to believe that the problem is machine-specific and/or related to the recompilation of code.

I've basically exhausted all Google search results that I can find that cover a similar issue. Per the Google results, I have attempted adding the overloaded version of Reponse.Redirect, using window.location via JavaScript, and FormsAuthentication.RedirectFromLoginPage. I have also used numCompilesBeforeAppRestart and modified the session timeout value in the Web.config file. None of those methods appeared to help.

If anyone has any other suggestions, I would be more than happy to hear from you! Thanks again.
 

One thing you can try is to create a dummy session object in the Session Start method in a global.ascx file. I don't know why, but in some instances the session get's confused and I've found creating something in session "primes" the whole process. Just a thought.


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top