[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

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.
0
taz8020
Asked:
taz8020
  • 7
  • 6
  • 5
  • +1
1 Solution
 
EyalCommented:
do you use static variables somehow in your code?
0
 
taz8020Author Commented:
sorry I new to this. where do you mean? anyware in my code or just in the cart section?
0
 
Paul MacDonaldDirector, Information SystemsCommented:
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
EyalCommented:
in the cart code
0
 
taz8020Author Commented:
Hi Eval, no not even in the entire stie. Why would this have caused a problem? do you have any other ideas?
0
 
EyalCommented:
can you post the codes that adds and gets from the cart?
0
 
Paul MacDonaldDirector, Information SystemsCommented:
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?
0
 
taz8020Author Commented:
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

0
 
CodeCruiserCommented:
What session state mode are you using?
0
 
taz8020Author Commented:
Hi Codecruiser, how do I find out that?
0
 
Paul MacDonaldDirector, Information SystemsCommented:
Change this bit of code (make sure you save the current version) and see if anything changes:

#Region "Singleton Implementation"
  ' Readonly variables can only be set in initialization or in a constructor
  Private Shared _Instance As New ShoppingCart

  ' The static constructor is called as soon as the class is loaded into memory
  Public Shared Function Instance() As ShoppingCart
    ' 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 Function


By the way, I use this exact same cart - heavily modified for my own purposes - and it works great.  In any case, it looks as if the same instance is being shared by all sessions and thats (obviously) not the behavior you want.  See if the minor changes in my example fix that problem.
0
 
taz8020Author Commented:
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.
0
 
CodeCruiserCommented:
>Private Shared _Instance As New ShoppingCart

I dont think that is the way to do it for singletons.
0
 
Paul MacDonaldDirector, Information SystemsCommented:
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.  
0
 
taz8020Author Commented:
CodeCruiser, why do you think its not a good way. It worked fine?
0
 
taz8020Author Commented:
Thanks  Paul
Ok just opened a new thread if anyone can help
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27388677.html
0
 
CodeCruiserCommented:
>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.
0
 
Paul MacDonaldDirector, Information SystemsCommented:
@CodeCruiser - No, the code looks to see if it exists in the session an creates it only if it doesn't.
0
 
CodeCruiserCommented:
>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.
0
 
CodeCruiserCommented:
But if its working, I guess it does not matter.
0
 
Paul MacDonaldDirector, Information SystemsCommented:
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!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now