Link to home
Start Free TrialLog in
Avatar of SillySoumare
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(lobjContext.Session("TestShoppingCartID"))

            If fsCartID = "" Then
                Dim lobjCartID As Guid = Guid.NewGuid

                lobjContext.Session("TestShoppingCartID") = lobjCartID.ToString
                fsCartID = lobjContext.Session("TestShoppingCartID").ToString
            End If
        End If
    End Sub
End Class
Avatar of Bane83
Bane83
Flag of Canada image

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).
Avatar of SillySoumare
SillySoumare

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(lobjContext.Session("TestShoppingCartID"))

            If fsCartID = "" Then
                Dim lobjCartID As Guid = Guid.NewGuid

                lobjContext.Session("TestShoppingCartID") = lobjCartID.ToString
                fsCartID = lobjContext.Session("TestShoppingCartID").ToString
            End If
        End If
    End Sub
End Class
Also, try removing the ReadOnly from ShoppingCartID.
I removed the read only and it is still doing the same thing.  
ASKER CERTIFIED SOLUTION
Avatar of Bane83
Bane83
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You rock