taz8020
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.
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.
do you use static variables somehow in your code?
ASKER
sorry I new to this. where do you mean? anyware in my code or just in the cart section?
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
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?
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?
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.Sessio n("ASPNETS hoppingCar t") Is Nothing Then
Instance = New ShoppingCart()
Instance.Items = New List(Of CartItem)
HttpContext.Current.Sessio n("ASPNETS hoppingCar t") = Instance
Else
Instance = CType(HttpContext.Current. Session("A SPNETShopp ingCart"), 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(pro ductRef) 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(pro ductRef) 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
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.Sessio
Instance = New ShoppingCart()
Instance.Items = New List(Of CartItem)
HttpContext.Current.Sessio
Else
Instance = CType(HttpContext.Current.
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(pro
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(pro
'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?
ASKER
Hi Codecruiser, how do I find out that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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.
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.
ASKER
CodeCruiser, why do you think its not a good way. It worked fine?
ASKER
Thanks Paul
Ok just opened a new thread if anyone can help
https://www.experts-exchange.com/questions/27388677/ASP-NET-How-you-use-access-a-vaule-used-on-the-main-form-from-within-a-user-control.html
Ok just opened a new thread if anyone can help
https://www.experts-exchange.com/questions/27388677/ASP-NET-How-you-use-access-a-vaule-used-on-the-main-form-from-within-a-user-control.html
>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.
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.
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!