Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!!

i have made a session naming "Cart". now whenever a user adds item to cart application checks the whether the session "Cart" is null or not . if null it creates the session if not it adds items to the session.
The problem that i am facing is that if simultaneously two user open the application and one adds item to it's cart the item also gets shown in the other users cart.

what should i do to remove this error ?? it's urgent... :'(
Posted
Comments
Mario Majčica 3-Feb-12 4:43am    
Can you show us the code you are using for storing data to the session?

Cheers
ujjwal uniyal 3-Feb-12 4:54am    
static ShoppingCart()
{

if (HttpContext.Current.Session["Cart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<cartitem>();
HttpContext.Current.Session["Cart"] = Instance;
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["Cart"];
}
}

At a guess (and without code that is all I can do) you are creating your own sessions, presumably as a file, or part of a database.

Don't.
Use either the actual Session object, or (better if you can) cookies.

Either way is specific to the user PC or connection and will isolate him from other users
 
Share this answer
 
Comments
ujjwal uniyal 3-Feb-12 4:52am    
static ShoppingCart()
{
if (HttpContext.Current.Session["Cart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<cartitem>();
HttpContext.Current.Session["Cart"] = Instance;
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["Cart"];
}
}

because i have not given any particular unique id to session for every user may be that's why i am facing this problem.

i tried cookie instead of session here but it showed me error when i set the cookie value to instance.
OriginalGriff 3-Feb-12 5:06am    
What are you doing? Why is that static? Did you expect that to help? Get rid of the static stuff - it can't access the current user info...
ujjwal uniyal 3-Feb-12 5:26am    
i have used class files. now since i wanted to check whether the session exists or not i used static.. Here is the class file..


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public class ShoppingCart
{
#region Properties

public List<cartitem> Items { get; private set; }

#endregion

#region Singleton Implementation


public static readonly ShoppingCart Instance;

ShoppingCart()
{
if (HttpContext.Current.Session["Cart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<cartitem>();
HttpContext.Current.Session["Cart"] = Instance;
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["Cart"];
}
}

protected ShoppingCart() { }

#endregion

#region Item Modification Methods

public void AddItem( string pid)
{
CartItem newItem = new CartItem(pid);

if (Items.Contains(newItem))
{
foreach (CartItem item in Items)
{
if (item.Equals(newItem))
{
item.Quantity++;
return;
}
}
}
else
{
newItem.Quantity = 1;
Items.Add(newItem);
}
}

public void SetItemQuantity( string pid, int quantity)
{
if (quantity == 0)
{
RemoveItem(pid);
return;
}
CartItem updatedItem = new CartItem(pid);

foreach (CartItem item in Items)
{
if (item.Equals(updatedItem))
{
item.Quantity = quantity;
return;
}
}
}

public void RemoveItem( string pid)
{
CartItem removedItem = new CartItem(pid);
Items.Remove(removedItem);
}


#endregion

#region Reporting Methods
public decimal GetSubTotal()
{
decimal subTotal = 0;
foreach (CartItem item in Items)
subTotal += item.TotalPrice + item.Tax ;

return subTotal;
}
#endregion
}
OriginalGriff 3-Feb-12 5:42am    
The session is instance specific. If you start using static information then *BY DESIGN* it is shared between all class instances within the application. Which, for web site projects means in practice that it may be (but is not guaranteed to be) shared between a number of users. Get rid of the static - it will only cause you massive, unpredictable, and intermittent problems.
hai ujjwal,

is that session state or aplication state bcos particular session state is avaiable only for that user not for all
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900