SillySoumare
asked on
Session Management
I have the code below that creates a session everytime the website is loaded from the user's pc. It works fine on the development machine, but when I upload the site to production. It creates only one session from the first pc I opened the website, but when I use another pc to open the site it uses the same session that was created from the other pc. I want the site to create a new session everytime the user loads it regardless from which pc.
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
Private Shared fsCartID As String
Public Shared ReadOnly Property ShoppingCartId() As String
Get
Return fsCartID
End Get
End Property
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim lobjContext As HttpContext = HttpContext.Current
If ShoppingCartId Is Nothing Then
fsCartID = Convert.ToString(lobjConte xt.Session ("TestShop pingCartID "))
If fsCartID = "" Then
Dim lobjCartID As Guid = Guid.NewGuid
lobjContext.Session("TestS hoppingCar tID") = lobjCartID.ToString
fsCartID = lobjContext.Session("TestS hoppingCar tID").ToSt ring
End If
End If
End Sub
End Class
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
Private Shared fsCartID As String
Public Shared ReadOnly Property ShoppingCartId() As String
Get
Return fsCartID
End Get
End Property
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim lobjContext As HttpContext = HttpContext.Current
If ShoppingCartId Is Nothing Then
fsCartID = Convert.ToString(lobjConte
If fsCartID = "" Then
Dim lobjCartID As Guid = Guid.NewGuid
lobjContext.Session("TestS
fsCartID = lobjContext.Session("TestS
End If
End If
End Sub
End Class
You're a little mistaken. It's not that it's using the same session, it's because you marked fsCartID as a Shared variable. The class will only create this variable one time and use it for all users who access the site (with the same cartID every time).
ASKER
I changed it to the code below but I'm still getting the same session, no matter what pc the website is running. Can you help me change the session when the site is reopened from another browser?
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
Private fsCartID As String
Public ReadOnly Property ShoppingCartId() As String
Get
Return fsCartID
End Get
End Property
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim lobjContext As HttpContext = HttpContext.Current
If ShoppingCartId Is Nothing Then
fsCartID = Convert.ToString(lobjConte xt.Session ("TestShop pingCartID "))
If fsCartID = "" Then
Dim lobjCartID As Guid = Guid.NewGuid
lobjContext.Session("TestS hoppingCar tID") = lobjCartID.ToString
fsCartID = lobjContext.Session("TestS hoppingCar tID").ToSt ring
End If
End If
End Sub
End Class
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
Private fsCartID As String
Public ReadOnly Property ShoppingCartId() As String
Get
Return fsCartID
End Get
End Property
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim lobjContext As HttpContext = HttpContext.Current
If ShoppingCartId Is Nothing Then
fsCartID = Convert.ToString(lobjConte
If fsCartID = "" Then
Dim lobjCartID As Guid = Guid.NewGuid
lobjContext.Session("TestS
fsCartID = lobjContext.Session("TestS
End If
End If
End Sub
End Class
Also, try removing the ReadOnly from ShoppingCartID.
ASKER
I removed the read only and it is still doing the same thing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You rock