[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Session Management

Posted on 2008-01-29
6
Medium Priority
?
884 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:SillySoumare
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:Bane83
ID: 20774303
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).
0
 

Author Comment

by:SillySoumare
ID: 20774674
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
0
 
LVL 10

Expert Comment

by:Bane83
ID: 20774882
Also, try removing the ReadOnly from ShoppingCartID.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:SillySoumare
ID: 20774942
I removed the read only and it is still doing the same thing.  
0
 
LVL 10

Accepted Solution

by:
Bane83 earned 200 total points
ID: 20777803
I was thinking about this last night before I went to sleep and I believe I figured out your problem.
It didn't hit me that you are doing this in the Global.asax which belongs to the application and is not created each time a user browses to the site.  This means that regardless of not using Shared, the cartID is being created and is static across all sessions because it belongs to the Global.asax.

Your fix is below.

Next, when you want to reference their cart ID you grab it directly from session:
Convert.ToString(Session("TestShoppingCartID"))
Imports System.Web.SessionState
 
Public Class Global_asax
    Inherits System.Web.HttpApplication
 
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim lobjContext As HttpContext = HttpContext.Current
 
        If lobjContext.Session("TestShoppingCartID") Is Nothing Then
            lobjContext.Session("TestShoppingCartID") = System.GUID.NewGuid().ToString();
 
        End If
    End Sub
End Class

Open in new window

0
 

Author Closing Comment

by:SillySoumare
ID: 31426274
You rock
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question