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 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!