Link to home
Start Free TrialLog in
Avatar of taz8020
taz8020Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Asp.net session varible is showing to all uesrs

Hi I am new to asp.net but I have a session varible that stors the items in the cart. I though it was all going well until I tried it on another computer and seen all the items I had added on the fiirst computer were in the cart on the second.

So to sum iit up the Asp.net session varible is showing to all uesrs
I dont know if this is a coading error or becuase its on a web farm thing.
Please help me.
Avatar of Eyal
Eyal
Flag of Israel image

do you use static variables somehow in your code?
Avatar of taz8020

ASKER

sorry I new to this. where do you mean? anyware in my code or just in the cart section?
Avatar of Paul MacDonald
Is the information only in a session-level variable?  Are you sure you're not storing it in an application-level variable?  Are you storing something in a database and retrieving it?
in the cart code
Avatar of taz8020

ASKER

Hi Eval, no not even in the entire stie. Why would this have caused a problem? do you have any other ideas?
can you post the codes that adds and gets from the cart?
Did you develop the cart yourself, or is it something you bought?  

Is it a class that gets instantiated?  If so, does it exist as a singleton implementation?  That is, does it exist as something that belongs to the session instance?
Avatar of taz8020

ASKER

Hi below is the code

Imports Microsoft.VisualBasic

' The ShoppingCart class
' Holds the items that are in the cart and provides methods for their manipulation
Public Class ShoppingCart

#Region "Properties"

    Private _items As List(Of CartItem)
    Public Property Items() As List(Of CartItem)
        Get
            Return _items
        End Get
        Private Set(ByVal value As List(Of CartItem))
            _items = value
        End Set
    End Property

#End Region

#Region "Singleton Implementation"

    ' Readonly variables can only be set in initialization or in a constructor
    Public Shared ReadOnly Instance As ShoppingCart
    ' The static constructor is called as soon as the class is loaded into memory
    Shared Sub New()
        ' If the cart is not in the session, create one and put it there
        ' Otherwise, get it from the session
        If HttpContext.Current.Session("ASPNETShoppingCart") Is Nothing Then
            Instance = New ShoppingCart()
            Instance.Items = New List(Of CartItem)
            HttpContext.Current.Session("ASPNETShoppingCart") = Instance
        Else
            Instance = CType(HttpContext.Current.Session("ASPNETShoppingCart"), ShoppingCart)
        End If

    End Sub

    ' A protected constructor ensures that an object can't be created from outside
    Protected Sub New()

    End Sub

#End Region

#Region "Item Modification Methods"

    ' AddItem() - Adds an item to the shopping
    Public Sub AddItem(ByVal NewItem As CartItem)
        ' Create a new item to add to the cart
        'Dim newItem = New CartItem(productRef)
        'newItem.ProductRef = "P5"
        'newItem.Description = "ghjghjg"
        'newItem.Quantity = 100
        'newItem.UnitPrice = 0.745
        'newItem.ItemWeight = 0.5

        ' If this item already exists in our list of items, increase the quantity
        ' Otherwise, add the new item to the list
        If Items.Contains(newItem) Then
            For Each item As CartItem In Items
                If item.Equals(newItem) Then
                    item.Quantity += 1
                    Return
                End If
            Next
        Else
            'newItem.Quantity = 1
            Items.Add(newItem)
        End If

    End Sub

    ' SetItemQuantity() - Changes the quantity of an item in the cart
    Public Sub SetItemQuantity(ByVal productRef As String, ByVal quantity As Integer)
        ' If we are setting the quantity to 0, remove the item entirely
        If quantity = 0 Then
            RemoveItem(productRef)
            Return
        End If

        ' Find the item and update the quantity
        Dim updatedItem = New CartItem(productRef)
        For Each item As CartItem In Items
            'If item.Equals(updatedItem) Then
            If item.ProductRef.Equals(productRef) Then
                item.Quantity = quantity
                Return
            End If
        Next
    End Sub

    ' RemoveItem() - Removes an item from the shopping cart
    Public Sub RemoveItem(ByVal productRef As String)
        Dim removedItem = New CartItem(productRef)

        For Each item As CartItem In Items
            If item.ProductRef.Equals(productRef) Then
                'If item.Equals(removedItem) Then
                Items.Remove(item)
                Return
            End If
        Next
    End Sub

#End Region

#Region "Reporting Methods"

    ' GetSubTotal() - returns the total price of all of the items before tax, shipping, etc.
    Public Function GetSubTotal() As Decimal
        Dim subTotal As Decimal = 0
        For Each item As CartItem In Items
            subTotal += item.TotalPrice
        Next

        Return subTotal
    End Function

#End Region

End Class

What session state mode are you using?
Avatar of taz8020

ASKER

Hi Codecruiser, how do I find out that?
ASKER CERTIFIED SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America 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
Avatar of taz8020

ASKER

Paul, you are a star. Thank you so much. As you have used this cart before do you think it is the best way of using a shopping cart?
I have just read that some people have problems on web farm where they loose the session every 60mins.
>Private Shared _Instance As New ShoppingCart

I dont think that is the way to do it for singletons.
It's a solid piece of code and it works really well.  As I said, I've modified mine extensively and not broken it (permanently!) yet, so it must be pretty resilient!

My sessions time out after 20 minutes - the default.  I'm okay with that, but that may be a problem for other folks.  I'm not using the cart in a farm, per se, but in a set of servers that are managed using load balancing.  
Avatar of taz8020

ASKER

CodeCruiser, why do you think its not a good way. It worked fine?
>CodeCruiser, why do you think its not a good way. It worked fine?

In singleton, object is instantiated only if it has not been instantiated yet. In the code, you are instantiating it anyway.
@CodeCruiser - No, the code looks to see if it exists in the session an creates it only if it doesn't.
>No, the code looks to see if it exists in the session an creates it only if it doesn't.

You changed

Private Shared _Instance As ShoppingCart

to

Private Shared _Instance As New ShoppingCart

so it creates an instance regardless.
But if its working, I guess it does not matter.
Yes, it creates a new instance in each session (if an instance doesn't exist yet).  FWIW, I don't claim to understand it, I just make it work!