Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all,

i am a mca student and i was trying to prepare my collage project. to make my project more realistic i want to add a cart to my project. which i am not aware how to do. in order to get the ans to my question i gone through one article and as per the guidance i have a prepared a demo sales cart.

cart was properly functional. problem occurred when i uploaded my demo project on free hosting site. i found that cart is not empty. when i updated the information in the cart from computer A was also appearing in the computer B. i think same cart is used between all users.

below is what i have created, i want some to review this code. i know i am asking to much, but your help will definitely help in getting good grade.

first i created 3 classes: cartitem, product, shoppingcart. and code is as follows:

1) product class:
public class Product
    public int Id { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }

    public Product(int id)
        this.Id = id;
        switch (id) {
            case 1:
                this.Price = 19.95m;
                this.Description = "Shoes";
            case 2:
                this.Price = 9.95m;
                this.Description = "Shirt";
            case 3:
                this.Price = 14.95m;
                this.Description = "Pants";

2) Shopping cart
public class ShoppingCart {
	#region Properties
	public List<cartitem> Items { get; private set; }

	#region Singleton Implementation

	// Readonly properties can only be set in initialization or in a constructor
	public static readonly ShoppingCart Instance;
	// The static constructor is called as soon as the class is loaded into memory
	static 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"] == null) {
			Instance = new ShoppingCart();
			Instance.Items = new List<cartitem>();
			HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
		} else {
			Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];

	// A protected constructor ensures that an object can't be created from outside
	protected ShoppingCart() { }


	#region Item Modification Methods
	 * AddItem() - Adds an item to the shopping 
	public void AddItem(int productId) {
		// Create a new item to add to the cart
		CartItem newItem = new CartItem(productId);

		// 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)) {
			foreach (CartItem item in Items) {
				if (item.Equals(newItem)) {
		} else {
			newItem.Quantity = 1;

3) cartitem class
public class CartItem : IEquatable<cartitem> {
	#region Properties

	// A place to store the quantity in the cart
	// This property has an implicit getter and setter.
	public int Quantity { get; set; }

	private int _productId;
	public int ProductId {
		get { return _productId; }
		set {
			// To ensure that the Prod object will be re-created
			_product = null;
			_productId = value;

	private Product _product = null;
	public Product Prod {
		get {
			// Lazy initialization - the object won't be created until it is needed
			if (_product == null) {
				_product = new Product(ProductId);
			return _product;

	public string Description {
		get { return Prod.Description; }

	public decimal UnitPrice {
		get { return Prod.Price; }

	public decimal TotalPrice {
		get { return UnitPrice * Quantity; }


	// CartItem constructor just needs a productId
	public CartItem(int productId) {
		this.ProductId = productId;

	 * Equals() - Needed to implement the IEquatable interface
	 *    Tests whether or not this item is equal to the parameter
	 *    This method is called by the Contains() method in the List class
	 *    We used this Contains() method in the ShoppingCart AddItem() method
	public bool Equals(CartItem item) {
		return item.ProductId == this.ProductId;

Note: I have also created the web page which have link button. code is as follows
protected void btnAddShoes_Click(object sender, EventArgs e) {
        // Add product 1 to the shopping cart

        // Redirect the user to view their shopping cart

Can any one please review the code and advise what is wrong init
Updated 6-Jan-13 2:59am

1 solution

If you use a Singleton instance of a shopping cart, then I am not surprised if it is shared between several computers.
I would instead save the Products to the Session (or to cookies) as this is unique to the connection / PC.

Cookies are more friendly for coustomers - If I loose the connection half way through I'd be annoyed if my shop-to-date vanished as well.
Share this answer
jonlink01 6-Jan-13 10:19am    
Hi Griff,

Thank you for coming forward and helping me again:-)

i have used the session in creating the cart in above demo project.

can you please share some online tutorial for creating a cart with the help of cookies
jonlink01 4-Feb-13 16:51pm    
Hi Griff,

could you please help me in completing my project.
OriginalGriff 5-Feb-13 3:32am    
Should tell you what you need to know, and includes examples.

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