Shopping Cart in ASP.NET

Build a Shopping Cart in ASP.NET

Nov 17th, 2008 in Other by Don Wilson

Shopping carts are very important and can many times be the most intimidating part of building an e-commerce site. This tutorial will show how easy it can be to implement a shopping cart using ASP.NET. Additionally, several basic explanations will be provided to help beginning ASP.NET programmers understand this wonderful framework.

PG

Author: Don Wilson

I'm a web designer and a software developer who loves both design and development. I do web development for KLAS Enterprises and I've recently started making themes for ThemeForest on the side. I'm happily married to a gorgeous lady and we have an awesome son.

Quick Overview of ASP.NET

Since ASP.NET hasn't been covered too much on NETTUTS, I thought it would be good to include a brief overview of some of the things that distinguish it from other languages.

  1. Code is compiled. The first time an ASP.NET page is requested over the web, the code is compiled into one or more DLL files on the server. This gives you the ability to just copy code out to the server and it gives you the speed benefit of compiled code.
  2. ASP.NET is an object oriented framework. Every function, property and page is part of a class. For example, each web page is its own class that extends the Page class. The Page class has an event that is fired when the webpage is loaded called the "Page Load Event". You can write a function that subscribes to that event and is called on. The same principle applies to other events like the button click and "drop-down" "selected index changed" events.
  3. The logic is separate from the design and content. They interact with each other, but they are in separate places. Generally, this allows a designer to design without worrying about function and it allows the programmer to focus on function without looking at the design. You have the choice of putting them both in the same file or in different files. This is similar to model-view-controller model.

If you are new to ASP.NET (and you have Windows), you can try it out for free You can download Visual Studio Express by visiting the ASP.NET website. Also, when you create a website locally on your machine, you can run the website at any time and Visual Studio will quickly start a server on your computer and pull up your website in your default browser.

Step 1: Create the ShoppingCart Class

We need a place to store the items in the shopping cart as well as functions to manipulate the items. We'll create a ShoppingCart class for this. This class will also manage session storage.

First, we have to create the App_Code folder. To do this, go to the "Website" menu, then "Add ASP.NET Folder", and choose "App_Code." This is where we'll put all of our custom classes. These classes will automatically be accessible from the code in any of our pages (we don't need to reference it using something similar to "include" or anything). Then we can add a class to that folder by right-clicking on the folder and choosing "Add New Item."

Quick Tip: Regions in ASP.NET are really nice to organize and group code together. The nicest thing about them is that you can open and close regions to minimize the amount of code that you are looking at or quickly find your way around a file.

using System.Collections.Generic;
using System.Web;

/**
 * The ShoppingCart class
 * 
 * Holds the items that are in the cart and provides methods for their manipulation
 */
public class ShoppingCart {
    #region Properties
    
    public List<CartItem> Items { get; private set; }
    
    #endregion

    #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() { }

    #endregion

    #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)) {
                    item.Quantity++;
                    return;
                }
            }
        } else {
            newItem.Quantity = 1;
            Items.Add(newItem);
        }
    }

    /**
     * SetItemQuantity() - Changes the quantity of an item in the cart
     */
    public void SetItemQuantity(int productId, int quantity) {
        // If we are setting the quantity to 0, remove the item entirely
        if (quantity == 0) {
            RemoveItem(productId);
            return;
        }

        // Find the item and update the quantity
        CartItem updatedItem = new CartItem(productId);

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

    /**
     * RemoveItem() - Removes an item from the shopping cart
     */
    public void RemoveItem(int productId) {
        CartItem removedItem = new CartItem(productId);
        Items.Remove(removedItem);
    }
    #endregion

    #region Reporting Methods
    /**
     * GetSubTotal() - returns the total price of all of the items
     *                 before tax, shipping, etc.
     */
    public decimal GetSubTotal() {
        decimal subTotal = 0;
        foreach (CartItem item in Items)
            subTotal += item.TotalPrice;

        return subTotal;
    }
    #endregion
}

Step 2: The CartItem & Product Classes

With a place to store our shopping cart items, we need to be able to store information about each item. We'll create a CartItem class that will do this. We'll also create a simple Product class that will simulate a way to grab data about the products we're selling.

The CartItem class:

using System;

/**
 * The CartItem Class
 * 
 * Basically a structure for holding item data
 */
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; }
    }

    #endregion

    // 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;
    }
}

The Product class:

/**
 * The Product class
 * 
 * This is just to simulate some way of accessing data about  our products
 */
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";
                break;
            case 2:
                this.Price = 9.95m;
                this.Description = "Shirt";
                break;
            case 3:
                this.Price = 14.95m;
                this.Description = "Pants";
                break;
        }
    }

}

Definition: A "property" in ASP.NET is a variable in a class that has a setter, a getter, or both. This is similar to other languages, but in ASP.NET, the word property refers specifically to this. An example of this is the ProductId property in the CartItem class. It is not simply a variable in a class with a method to get or set it. It is declared in a special way with get{} and set{} blocks.

Let's Add Items to the Cart

After having our heads in the code for so long, it's time we do something visual. This page will simply be a way to add items to the cart. All we need is a few items with "Add to Cart" links. Let's put this code in the Default.aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My Store</title>
    <link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>

    <form id="form1" runat="server">
        
        <div class="container">
            <h1>My Store</h1>
            
            <div class="products">
                <div>Shoes - <asp:LinkButton runat="server" ID="btnAddShirt" OnClick="btnAddShoes_Click">Add To Cart</asp:LinkButton></div>

                <div>Shirt - <asp:LinkButton runat="server" ID="btnAddShorts" OnClick="btnAddShirt_Click">Add To Cart</asp:LinkButton></div>
                <div>Pants - <asp:LinkButton runat="server" ID="btnAddShoes" OnClick="btnAddPants_Click">Add To Cart</asp:LinkButton></div>
            </div>

            
            <a href="ViewCart.aspx">View Cart</a>
        </div>
        
    </form>
</body>
</html>

As you can see, the only thing happening here is that we have a few LinkButtons that have OnClick event handlers associated to them.

In the code-behind page, we have 4 event handlers. We have one for each LinkButton that just adds an item to the shopping cart and redirects the user to view their cart. We also have a Page_Load event handler which is created by the IDE by default that we didn't need to use.

using System;

public partial class _Default : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {

    }

    protected void btnAddShoes_Click(object sender, EventArgs e) {
        // Add product 1 to the shopping cart
        ShoppingCart.Instance.AddItem(1);

        // Redirect the user to view their shopping cart
        Response.Redirect("ViewCart.aspx");
    }

    protected void btnAddShirt_Click(object sender, EventArgs e) {
        ShoppingCart.Instance.AddItem(2);
        Response.Redirect("ViewCart.aspx");
    }

    protected void btnAddPants_Click(object sender, EventArgs e) {
        ShoppingCart.Instance.AddItem(3);
        Response.Redirect("ViewCart.aspx");
    }

}

Build the Shopping Cart Page

Finally, what we've been preparing for the whole time—the shopping cart! Let's just look at ViewCart.aspx first and I'll explain it after that.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewCart.aspx.cs" Inherits="ViewCart" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Shopping Cart</title>
    <link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>

    <form id="form1" runat="server">
        <div class="container">
            <h1>Shopping Cart</h1>
            <a href="Default.aspx">< Back to Products</a>

            <br /><br />
            <asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." GridLines="None" Width="100%" CellPadding="5" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">
                <HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" />
                <FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" />
                <AlternatingRowStyle BackColor="#F8F8F8" />
                <Columns>

                    <asp:BoundField DataField="Description" HeaderText="Description" />
                    <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                            <asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />
                            <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>' style="font-size:12px;"></asp:LinkButton>

                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="UnitPrice" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
                    <asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
                </Columns>
            </asp:GridView>

            <br />
            <asp:Button runat="server" ID="btnUpdateCart" Text="Update Cart" OnClick="btnUpdateCart_Click" />
        </div>
    </form>
</body>
</html>

The GridView control is a powerful control that can seem complicated at first. I won't discuss the style elements because they are self-explanatory. (There are some principles here that I'm not going to explain in depth. I am just going to try to get the main idea across). Let's break it down.

  • Giving the GridView an ID will allow us to access the GridView from the code-behind using that ID.
    ID="gvShoppingCart"
  • The GridView will automatically generate columns and column names from the data that we supply unless we specifically tell it not to.
    AutoGenerateColumns="false"
  • We can tell the GridView what to display in case we supply it with no data.
    EmptyDataText="There is nothing in your shopping cart."
  • We want to show the footer so that we can display the total price.
    ShowFooter="true"
  • It will be nice for us to have an array of ProductIds indexed by the row index when we are updating the quantity of a cart item in the code-behind. This will do that for us:
    DataKeyNames="ProductId"
  • We need events to respond to two events: RowDataBound and RowCommand. Basically, RowDataBound is fired when the GridView takes a row of our data and adds it to the table. We are only using this event to respond to the footer being bound so that we can customize what we want displayed there. RowCommand is fired when a link or a button is clicked from inside the GridView. In this case, it is the "Remove" link.
    OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand"

Now let's talk about the columns. We define the columns here and the GridView will take every row in the data that we supply and map the data in that row to the column that it should display in. The simplest column is the BoundField. In our case, it is going to look for a "Description" property in our CartItem object and display it in the first column. The header for that column will also display "Description."

We needed the quantity to display inside a textbox rather than just displaying as text, so we used a TemplateField. The TemplateField allows you to put whatever you want in that column. If you need some data from the row, you just insert <%# Eval("PropertyName") %>. The LinkButton that we put in our TemplateField has a CommandName and a CommandArgument, both of which will be passed to our GridView's RowCommand event handler.

The last thing worth mentioning here is that the last two BoundFields have a DataFormatString specified. This is just one of the many format strings that ASP.NET provides. This one formats the number as a currency. See the Microsoft documentation for other format strings.

Now we can look at the code-behind page. I have supplied lots of comments here to describe what is happening.

The End Result:

Now we have a nice working shopping cart!

You Also Might Like...

Screencast

How to Search a Website Using ASP.NET 3.5 - screencast

Oct 1st in Screencasts by Jeffrey Way

I’m happy to say that today, we are posting our very first article on ASP.NET. In this screencast, I’ll show you how to implement a simple search functionality into your personal website. We’ll go over many of the new features in ASP.NET 3.5, such as LINQ and many of the AJAX controls that ship with Visual Studio/Web Developer.

Continue Reading

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Diogo Duarte November 17th

    Great tip…
    Gratz!

    ( Reply )
  2. PG

    Lavin November 17th

    Good tutorial, definitely helpful for people not wanting to use Shopping Cart software ro prefer ASP. Nice Work. I sure learned something from all this :D

    ( Reply )
  3. PG

    Vladimir November 17th

    Good solid post! Keep ‘em coming!

    ( Reply )
    1. PG

      Ponraj March 12th

      i got the Error message get/set must declare in body.
      the error msg is below:
      ‘CartItem.Quantity.get’ must declare a body because it is not marked abstract or extern
      any one help me Urgent please

      if you need more details ple see the link for the project

      http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/#add

      send me the solution soon…………

      Regards,
      Ponraj.S

      ( Reply )
  4. PG

    JHAY November 17th

    Great tut indeed! Thanks

    JHAY

    ( Reply )
  5. PG

    Jeff November 17th

    Pretty good post. It is a very generic overview of some principles, definitley not real world ready. Is this going to be a series, if so it is always great to see how these things get fleshed out.

    Also, be carful with your singltons. I don’t hink your implementation is thread safe, also somthing like your shopping cart probably shouldn’t be a singlton. You are going to have multiple carts. I would sugeest somthing like a simple shopping cart class that has the list and say a total. Then push out the mothods to a ShoppingCartManager or repository, then if you want make that class a singleton.

    ( Reply )
  6. PG

    Roshan Bhattarai November 17th

    wow…thanks for sharing this with us though I’m not a great fan of ASP.NET

    ( Reply )
  7. PG

    Don November 17th

    Good point Jeff. It would be better if the Shopping Cart was not a singleton. Thanks for pointing that out.

    ( Reply )
  8. PG

    Peter November 17th

    Been meaning to get my hands dirty using ASP .NET now for a long time but was too intimidated, your article gave me the push I needed.

    Thanks

    ( Reply )
  9. PG

    Max November 17th

    Good tutorial to get to grips with, the asp.net web site has huge amounts of resources to help as well.

    ( Reply )
  10. PG

    insicdesigns November 17th

    wow! nice tutorial in ASP.net. Keep it up

    ( Reply )
  11. PG

    Furley November 17th

    nice tut. asp.net makes me cringe a little though. hated it in college. don’t use it now.

    ( Reply )
  12. PG

    Shane November 17th

    Nice to see another ASP.NET tutorial – thanks for writing it Don.

    When I’ve implemented a shopping cart, I’ve stored it in the ASP.NET application object as a C# generics dictionary (mapping user id to their list of items). This way I always have one per user, and I use ‘lock’ to ensure it is thread safe.

    Also, the GridView has a CssClass property. I use that for my styling, rather than having style information ‘inline’, since this is no better than using inline CSS.

    I also have a CSS control adapter that modifies the output of the GridView class to automatically output the ‘alt’ class on alternate rows.

    Two other small points – I’m not a fan of abbreviating controls like GridViews to gv – I prefer to keep the default naming convention – GridView1, and change to what’s appropriate, so in this example, it’d be GridViewShoppingCart. The abbreviations can become a little obtuse at times, and I prefer to be explicit with my naming. Though I understand this is just my personal preference – each to their own.

    I’m also not so keen on regions – before version 2 of the .NET framework, they were useful in ASP.NET as they hid the ‘designer generated code’, but that’s taken care of now with partial classes, so all the control members of the page class and so on are in {page}.designer.cs.

    I believe that code is there to be read, and regions just make things harder to get at, and things could be missed (though you can expand them all through the Visual Studio IDE, granted.)

    I’m glad there’s been another ASP.NET tutorial – I hope it shows people that it is a mature and powerful platform. I don’t want to start a ‘my language is better than your debate’, but I think that C# is the nicest language to work with.

    @Furley – I’m surprised you didn’t like ASP.NET, but each to their own I suppose ;)

    ( Reply )
  13. PG

    Moksha November 17th

    many thanks its wonderful to see asp.net tutorial after many PHP and rail. nice to see this one, thanks for sharing and putting your time.

    ( Reply )
  14. PG

    Uğur ÇELENK November 17th

    Many thanks.
    I want to see more Asp.NET articles/tutorials ;)

    ( Reply )
  15. PG

    Lamin November 17th

    More ASP.NET please. Thnx for the article BTW. -)

    ( Reply )
  16. PG

    Luca Del Tongo November 17th

    Hi Don, good article… just a couple of construct critics. In ASP.NET you are right to say that the logic is separate from design and code thus avoiding that old spaghetti code but IMHO model view controller really implies another level of indirection…. one thing i usually do is avoiding the use of tag. i’m look forward to see more asp.net tutorials!!!

    ( Reply )
  17. PG

    awake November 17th

    Is this for ASP.Net 2.0 or 3.5?

    ( Reply )
  18. PG

    John Downey November 17th

    Awesome tutorial… I’d also like to see more ASP.NET content. Maybe even some MVC or Silverlight?

    ( Reply )
  19. PG

    John Buchmann November 17th

    Thanks for the tutorial…

    This site needs some “getting started” type tutorials on ASP.NET. This tutorial goes into a complex subject matter and i think this will scare off anyone considering the framework. When you’re not at an advanced level, ASP.NET really is quite easy to use.

    ( Reply )
  20. PG

    Jeff November 18th

    @john i submitted a getting started with asp.net mvc a while ago. I don’t think they liked it cause it was mostly setup (for visual web developer) i am rewriting it it though to have more meat. we will see what happens

    ( Reply )
  21. PG

    moon November 18th

    Don–great tut. I do much simpler things with .NET and had lately been thinking about shopping cart implementation and CMS’ implementations as well.

    I will have to try this out and see what I can add to this. Definitely a great tut for an intermediate user. I haven’t seen too many basic .NET tuts here. I think programming data-driven sites with .NET are so simple to do… you can get a lot done with a little bit of work; I can certainly appreciate that.

    @Jeff–great point, might want to elaborate a bit on it for non-programmers.

    ( Reply )
  22. PG

    kareem November 23rd

    this is wonderful tutorial i will put acopy of this lesson on
    my site here
    http://www.as7ap4you.com

    ( Reply )
  23. PG

    Abdul Al Mamun(JOHN) November 24th

    HI ,THIS IS JOHN FROM BANGLADESH,NARAYANGANJ:

    I WAS AGOG WHEN I FOUND THIS. THIS IS GREAT!

    ( Reply )
  24. PG

    Adam November 28th

    Cool Tut

    ( Reply )
  25. PG

    chuchu December 2nd

    I can’t see the code behind file for the shopping cart. What’s happening?

    ( Reply )
  26. PG

    Mallioch December 5th

    A thought…

    @Shane – As was mentioned above, the singleton isn’t necessary. But unless I’m missing something in your description, the suggestion to put the shopping cart in the application object strikes me as a bad idea. That data does not automatically get cleaned up so user information in the application memory space will keep growing and growing and growing… To avoid that you would have to reset frequently or have some cleanup process, but that’s what you avoid by using Session like the example used above. That will at the very least get cleaned up whenever the session times out. The application object is primarily for application level data, settings, etc., not per session data.

    ( Reply )
  27. PG

    Ronnie December 17th

    I used this example for a shopping cart app for my class project.

    When I presented it in front of the class, I had everyone visit the store and populate a cart. Well, everyone was sharing the cart! I know this is my fault for not checking this… but why did this happen if the cart object is stored in session???

    ( Reply )
  28. PG

    Don December 19th

    It is shared because of the Singleton implementation. The Singleton idea isn’t a good idea for a shopping cart. So, take static off and instantiate the cart every time (it will still load from the session) to fix this problem.

    ( Reply )
    1. PG

      phred April 5th

      When i do this i get an error on line 29:

      Line 27: public ShoppingCart()
      Line 28: {
      Line 29: if (HttpContext.Current.Session["ShoppingCart"] == null)

      any ideas ?

      ( Reply )
  29. PG

    Mohamed Salama January 21st

    THERE is Big Error Will be happened
    all your user will share the same shopping cart
    because
    Objects stored in session state must be serializable if the mode is set to StateServer.

    ( Reply )
  30. PG

    Mohamed Salama January 21st

    i tried to make it serializable by adding line
    in the class
    [Serializable]

    but it didn’t work
    i hope you can test my issue and contact with me by mail
    mohamed.master@gmail.com

    ( Reply )
  31. PG

    Martin February 12th

    Hi Any chance someone can post some code of the amended version? I’ve tried to strip the singletopn implementation out but keep running into problems – I’m getting confuzzled.

    Cheers

    ( Reply )
  32. PG

    satheesh February 15th

    Really very helpful for other who loves to work in .net .

    ( Reply )
  33. PG

    John February 19th

    “Now we can look at the code-behind page. I have supplied lots of comments here to describe what is happening.”

    Am I the only one who cant see the code behind?

    ( Reply )
    1. PG

      Phoenix April 14th

      No, you’re not the only one.

      ( Reply )
      1. PG

        Phoenix April 14th

        FWIW, the missing code is in the downloadable sample (at the top of the page)

  34. PG

    Momin February 22nd

    Hello All

    When i copy and paste this whole coding at the end i am getting this error i would really appreciate if someone can help me out with this plzzzz :-(

    Compiler Error Message: CS1061: ‘ASP.viewcart_aspx’ does not contain a definition for ‘gvShoppingCart_RowDataBound’ and no extension method ‘gvShoppingCart_RowDataBound’ accepting a first argument of type ‘ASP.viewcart_aspx’ could be found (are you missing a using directive or an assembly reference?)

    ( Reply )
  35. PG

    rakesh February 24th

    i got a error .
    please give me solution

    error :
    Compiler Error Message: CS0161: ‘ShoppingCart.Items.get’: not all code paths return a value

    ( Reply )
  36. PG

    Van.Le March 6th

    Thank you a lot .This is a useful website for a student like me . It’s easy to understand.

    ( Reply )
  37. PG

    Ponraj March 12th

    i got the Error message get/set must declare in body.
    the error msg is below:

    ‘CartItem.Quantity.get’ must declare a body because it is not marked abstract or extern

    any one help me Urgent please

    ( Reply )
  38. PG

    Rich March 17th

    wow great Tutorial!
    thanks! (n_n)

    the visual studio that they using is 2008

    you should try guy’s it’s working fine

    ( Reply )
  39. PG

    chandradev March 20th

    hi
    it is working fine in vs 2008, but it is not working vs 2005. i also getting errors like “get/set must declare in body”.

    how to make it executable in vs 2005 ?

    ( Reply )
  40. PG

    test March 25th

    test

    ( Reply )
  41. PG

    Tomas Matejka March 29th

    thanks for this tutorial

    ( Reply )
  42. PG

    Justin March 31st

    Gr8 tutorial – Did anyone work out how to remove the Singleton implementation so the cart isn’t shared?

    ( Reply )
    1. PG

      Phoenix April 16th

      I , too, am trying to figure out how to make this work on a session-by-session basis. Any help would be appreciated.

      ( Reply )
  43. PG

    John K April 8th

    Hi, Good stuff here.

    But, also having the same issue as Momin had on Feb 22: (CS1061: ‘ASP.viewcart_aspx’ does not contain a definition for ‘gvShoppingCart_RowDataBound’ and no extension method ‘gvShoppingCart_RowDataBound’ accepting a first argument of type ‘ASP.viewcart_aspx’ could be found (are you missing a using directive or an assembly reference?))

    Was this resolved? Please share.

    Thanks

    ( Reply )
  44. PG

    mark April 10th

    its crap like this that leads to crap everywhere else
    A tutorial should come from a tutor and teach good, sound principles.
    All in all, a tutorial should WORK – This doesnt. it is really poor as a result.

    However, sloppy code presented enters the food chain, and other people pick it up and spread it like a disease.

    ( Reply )
  45. PG

    anil April 15th

    how can i use sql database so that price is retrieved from the databse.

    ( Reply )
  46. PG

    Phoenix April 17th

    I’ve had some success with this modification and limiting the cart to a single session. It works, but it’s not fully tested. No warrantee expressed or implied. Your mileage may vary.

    #Region “Singleton Implementation”

    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 classShoppingCart()
    Instance.Items = New List(Of classCartItem)
    HttpContext.Current.Session(“ASPNETShoppingCart”) = Instance
    Else
    Instance = CType(HttpContext.Current.Session(“ASPNETShoppingCart”), ShoppingCart)
    End If
    End Function

    ‘ A protected constructor ensures that an object can’t be created from outside
    Private Sub New()

    End Sub

    #End Region

    ( Reply )
  47. PG

    Don Wilson April 18th

    Here is what you would do to remove the Sinlgeton implementation:

    Replace lines 20-34 in the Shopping Cart class above with this:


    // The static constructor is called as soon as the class is loaded into memory
    public static ShoppingCart GetShoppingCart() {
    // If the cart is not in the session, create one and put it there
    if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
    ShoppingCart cart = new ShoppingCart();
    cart.Items = new List();
    HttpContext.Current.Session["ASPNETShoppingCart"] = cart;
    }

    return (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
    }

    Then, every time that you need to access it, replace this:

    ShoppingCart.Instance.AddItem(1);

    with this:

    ShoppingCart cart = ShoppingCart.getShoppingCart();
    cart.AddItem(1);

    ( Reply )
    1. PG

      Charles May 7th

      Hi Don,

      Could you please provide the VB version of this fix? I tried running it through the C# to VB converter but i get this error on line 28 of shoppingcart.vb

      Compiler Error Message: BC32042: Too few type arguments to ‘System.Collections.Generic.List(Of T)’.

      Thanks for your help

      ( Reply )
  48. PG

    Don Wilson April 18th

    Sorry I left this comment in that doesn’t apply anymore:
    // The static constructor is called as soon as the class is loaded into memory

    This works because the only place the cart is stored is in the session. It is not store in a static/shared variable.

    I apologize for all of the confusion in this. I’ve done several shopping carts in the past. The Singleton idea seemed like a cool way to implement it and I did it before I thought it through all the way. Hopefully, the code sample above will help.

    ( Reply )
  49. PG

    Bhanu April 25th

    Really its good one shopping cart………..

    ( Reply )
  50. PG

    QYR May 12th

    Here is one good open-source ASP.NET shopping cart:
    http://www.nopCommerce.com

    ( Reply )
  51. PG

    Shafiur Rahman May 23rd

    Hi

    Please suggest me how to bind the data bethought singleton.
    Code is given below.
    public static ShoppingCart GetShoppingCart() {
    // If the cart is not in the session, create one and put it there
    if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
    ShoppingCart cart = new ShoppingCart();
    cart.Items = new List();
    HttpContext.Current.Session["ASPNETShoppingCart"] = cart;
    }

    return (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
    }

    Then, every time that you need to access it, replace this:

    ShoppingCart.Instance.AddItem(1);

    with this:

    ShoppingCart cart = ShoppingCart.getShoppingCart();
    cart.AddItem(1);

    kind regards

    ( Reply )
  52. PG

    asdas May 30th

    not working even with singleton cut…

    ( Reply )
  53. PG

    hainv June 1st

    good code !Are you think that we are save Cart Items to Cookies ?

    ( Reply )
  54. PG

    hainv June 4th

    If do same you ,all customer will have same cart
    I’m modified:
    // The static constructor is called as soon as the class is loaded into memory
    public 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["ShoppingCart"] == null)
    {
    _items = new List();
    HttpContext.Current.Session["ShoppingCart"] = _items;
    } else {
    _items = (List)HttpContext.Current.Session["ShoppingCart"];
    }
    }

    When you call method, you can do as :( new ShoppingCart()).Items;

    protected void BindData() {
    // Let’s give the data to the GridView and let it work!
    // The GridView will take our cart items one by one and use the properties
    // that we declared as column names (DataFields)
    gvShoppingCart.DataSource = (new ShoppingCart()).Items;
    gvShoppingCart.DataBind();
    }

    ( Reply )
  55. PG

    Jon June 27th

    Hello Don,

    First off, thanks for providing this tutorial it has really aided my basic understanding of how to implement a shopping cart in ASP.NET.

    I’ve been trying to get rid of the Singleton implementation with the replacement code you supplied above however I am getting an “object reference not set to an instance of an object” error when running the source. This error gets thrown on the following statement’s completion:

    “if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)”

    Here’s the modified code region that’s causing this problem

    #region New Implementation

    public static readonly ShoppingCart Instance;
    // Readonly properties can only be set in initialization or in a constructor
    public static ShoppingCart GetShoppingCart()
    {
    // If the cart is not in the session, create one and put it there
    if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
    {
    ShoppingCart cart = new ShoppingCart();
    cart.Items = new List();
    HttpContext.Current.Session["ASPNETShoppingCart"] = cart;
    }

    return (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
    }

    Any help is much appreciated, thanks again for the tutorial
    I’ve been unable to figure out how to fix this so any help is much appreciated.

    ( Reply )
  56. PG

    arjun July 20th

    Thanks, it helps really.
    But I want product list i.e ProdId,ProdName,Price from database then, how to do.
    please suggest me the way.

    ( Reply )
  57. PG

    Joel August 19th

    Hello Don

    Thanks , is really good

    I need to show a produt list with : Id, Name, Price, Graph, and description , do you have some samples ?

    Regards

    ( Reply )
  58. PG

    Velibor September 5th

    Works fine. That’s what I looking for. Maybe some AJAX in this could improve interaction without separately web page for viewing cart. Shopping cart could be visible from some pop-up window etc. :D

    Thanks for great tut.

    ( Reply )
  59. PG

    Joe October 11th

    I dont really use ASP that much, but still pretty cool. :P

    ( Reply )
  60. PG

    Kris Crosby November 9th

    To make this work with multiple users here is the complete solution to make this work for all of us who are not programmers. First in C. You need the change the following files from the example: ShoppingCart.cs per comments above. Then Default.aspx.cs per comments above. and last ViewCart.aspx.cs anytime the instance was called.

    ( Reply )
    1. PG

      Kris Crosby November 9th

      //ViewCart.aspx.cs

      using System;
      using System.Web.UI.WebControls;
      /*************************** MEASURE LINE *****************************************/
      public partial class ViewCart : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {
      // ASP.NET keeps track of the state of all server controls
      // (like the GridView). Because of this, we don’t need to supply it with
      // the data every time the page loads. (No more re-checking checkboxes
      // that were checked or re-selecting the right item in the drop-down when
      // the page is submitted. It’s all done for you!)
      if (!IsPostBack)
      BindData();
      }

      protected void BindData()
      {
      // Let’s give the data to the GridView and let it work!
      // The GridView will take our cart items one by one and use the properties
      // that we declared as column names (DataFields)
      ShoppingCart cart = ShoppingCart.GetShoppingCart();
      gvShoppingCart.DataSource = cart.Items;
      gvShoppingCart.DataBind();
      }

      protected void gvShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e) {
      // If we are binding the footer row, let’s add in our total

      if (e.Row.RowType == DataControlRowType.Footer) {
      ShoppingCart cart = ShoppingCart.GetShoppingCart();
      e.Row.Cells[3].Text = “Total: ” + cart.GetSubTotal().ToString(“C”);
      }
      }

      /**
      * This is the method that responds to the Remove button’s click event
      */
      protected void gvShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e) {
      if (e.CommandName == “Remove”) {
      int productId = Convert.ToInt32(e.CommandArgument);
      ShoppingCart cart = ShoppingCart.GetShoppingCart();
      cart.RemoveItem(productId);
      }

      // We now have to re-setup the data so that the GridView doesn’t keep
      // displaying the old data
      BindData();
      }

      protected void btnUpdateCart_Click(object sender, EventArgs e) {
      foreach (GridViewRow row in gvShoppingCart.Rows) {
      if (row.RowType == DataControlRowType.DataRow) {
      // We’ll use a try catch block in case something other than a number is typed in
      // If so, we’ll just ignore it.
      try {
      // Get the productId from the GridView’s datakeys
      int productId = Convert.ToInt32(gvShoppingCart.DataKeys[row.RowIndex].Value);
      // Find the quantity TextBox and retrieve the value
      int quantity = int.Parse(((TextBox)row.Cells[1].FindControl(“txtQuantity”)).Text);
      ShoppingCart cart = ShoppingCart.GetShoppingCart();
      cart.SetItemQuantity(productId, quantity);
      } catch (FormatException) { }
      }
      }

      BindData();
      }
      }

      ( Reply )
  61. PG

    Kris Crosby November 9th

    //Default.aspx.cs

    using System;

    public partial class _Default : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {

    }

    protected void btnAddShoes_Click(object sender, EventArgs e) {
    // Add product 1 to the shopping cart
    ShoppingCart cart = ShoppingCart.GetShoppingCart();
    cart.AddItem(1);
    // Redirect the user to view their shopping cart
    Response.Redirect(“ViewCart.aspx”);
    }

    protected void btnAddShirt_Click(object sender, EventArgs e) {
    ShoppingCart cart = ShoppingCart.GetShoppingCart();
    cart.AddItem(2);
    Response.Redirect(“ViewCart.aspx”);
    }

    protected void btnAddPants_Click(object sender, EventArgs e) {
    ShoppingCart cart = ShoppingCart.GetShoppingCart();
    cart.AddItem(3);
    Response.Redirect(“ViewCart.aspx”);
    }

    }

    ( Reply )
  62. PG

    Kris Crosby November 9th

    For VB, I used a C#->VB converter for the changed code for the same 3 files (ShoppingCart.vb,Default.aspx.vb and ViewCart.aspx.vb) Someone should really update the example files. A shopping cart for one person doesn’t make any sense and dispite the good intention of the author, if your going to teach something which establishes yourself as an expert, teach it right, and fix your mistakes acurately if they are discovered later. I am no programmer and I have to fix this code for all you programmers out there.

    ( Reply )
    1. PG

      Kris Crosby November 9th

      ‘ ShoppingCart.vb
      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 “Implementation”

      ‘ Readonly variables can only be set in initialization or in a constructor
      Public Shared ReadOnly Instance As ShoppingCart

      Public Shared Function GetShoppingCart() As ShoppingCart
      ‘ If the cart is not in the session, create one and put it there
      If HttpContext.Current.Session(“ASPNETShoppingCart”) Is Nothing Then
      Dim cart As New ShoppingCart()
      cart.Items = New List(Of CartItem)()
      HttpContext.Current.Session(“ASPNETShoppingCart”) = cart
      End If

      Return (DirectCast(HttpContext.Current.Session(“ASPNETShoppingCart”), ShoppingCart))

      End Function

      #End Region

      #Region “Item Modification Methods”

      ‘ AddItem() – Adds an item to the shopping
      Public Sub AddItem(ByVal productId As Integer)
      ‘ Create a new item to add to the cart
      Dim 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) 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 productId As Integer, ByVal quantity As Integer)
      ‘ If we are setting the quantity to 0, remove the item entirely
      If quantity = 0 Then
      RemoveItem(productId)
      Return
      End If

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

      ‘ RemoveItem() – Removes an item from the shopping cart
      Public Sub RemoveItem(ByVal productId As Integer)
      Dim removedItem = New CartItem(productId)

      For Each item As CartItem In Items
      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

      ( Reply )
    2. PG

      Kris Crosby November 9th

      ‘ Default.aspx.vb
      ‘ 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 “Implementation”

      ‘ Readonly variables can only be set in initialization or in a constructor
      Public Shared ReadOnly Instance As ShoppingCart

      Public Shared Function GetShoppingCart() As ShoppingCart
      ‘ If the cart is not in the session, create one and put it there
      If HttpContext.Current.Session(“ASPNETShoppingCart”) Is Nothing Then
      Dim cart As New ShoppingCart()
      cart.Items = New List(Of CartItem)()
      HttpContext.Current.Session(“ASPNETShoppingCart”) = cart
      End If

      Return (DirectCast(HttpContext.Current.Session(“ASPNETShoppingCart”), ShoppingCart))

      End Function

      #End Region

      #Region “Item Modification Methods”

      ‘ AddItem() – Adds an item to the shopping
      Public Sub AddItem(ByVal productId As Integer)
      ‘ Create a new item to add to the cart
      Dim 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) 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 productId As Integer, ByVal quantity As Integer)
      ‘ If we are setting the quantity to 0, remove the item entirely
      If quantity = 0 Then
      RemoveItem(productId)
      Return
      End If

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

      ‘ RemoveItem() – Removes an item from the shopping cart
      Public Sub RemoveItem(ByVal productId As Integer)
      Dim removedItem = New CartItem(productId)

      For Each item As CartItem In Items
      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

      Partial Class _Default
      Inherits System.Web.UI.Page

      Protected Sub btnAddShoes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddShirt.Click
      ‘ Add product 1 to the shopping cart
      Dim cart As ShoppingCart = ShoppingCart.GetShoppingCart()
      cart.AddItem(1)

      ‘ Redirect the user to view their shopping cart
      Response.Redirect(“ViewCart.aspx”)
      End Sub

      Protected Sub btnAddShirt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddShorts.Click
      Dim cart As ShoppingCart = ShoppingCart.GetShoppingCart()
      cart.AddItem(2)
      Response.Redirect(“ViewCart.aspx”)
      End Sub

      Protected Sub btnAddPants_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddShoes.Click
      Dim cart As ShoppingCart = ShoppingCart.GetShoppingCart()
      cart.AddItem(3)
      Response.Redirect(“ViewCart.aspx”)
      End Sub
      End Class

      ( Reply )
    3. PG

      Kris Crosby November 9th

      ‘ ViewCart.aspx.vb

      Partial Class ViewCart
      Inherits System.Web.UI.Page

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      ‘ ASP.NET keeps track of the state of all server controls
      ‘ (like the GridView). Because of this, we don’t need to supply it with
      ‘ the data every time the page loads. (No more re-checking checkboxes
      ‘ that were checked or re-selecting the right item in the drop-down when
      ‘ the page Is submitted. It’s all done for you!)

      If Not IsPostBack Then
      BindData()
      End If
      End Sub

      Protected Sub BindData()
      ‘ Let’s give the data to the GridView and let it work!
      ‘ The GridView will take our cart items one by one and use the properties
      ‘ that we declared as column names (DataFields)
      Dim cart As ShoppingCart = ShoppingCart.GetShoppingCart()
      gvShoppingCart.DataSource = cart.Items
      gvShoppingCart.DataBind()
      End Sub

      Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound
      ‘ If we are binding the footer row, let’s add in our total
      If e.Row.RowType = DataControlRowType.Footer Then
      Dim cart As ShoppingCart = ShoppingCart.GetShoppingCart()
      e.Row.Cells(3).Text = “Total: ” & cart.GetSubTotal().ToString(“C”)
      End If
      End Sub

      ‘ This is the method that responds to the Remove button’s click event
      Protected Sub gvShoppingCart_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvShoppingCart.RowCommand
      If e.CommandName = “Remove” Then
      Dim cart As ShoppingCart = ShoppingCart.GetShoppingCart()
      Dim productId = Convert.ToInt32(e.CommandArgument)
      cart.RemoveItem(productId)
      End If

      ‘ We now have to re-setup the data so that the GridView doesn’t keep displaying the old data
      BindData()
      End Sub

      End Class

      ( Reply )
  63. PG

    Yanayaya November 24th

    Reading over the comments are we now at a point where this code works and can allow multiple users to place orders? Also I note that this tutorial uses static values in a drop down for price and item.

    What would be the coding to gather that information from a database??

    ( Reply )
  64. PG

    Rakesh Prasad December 3rd

    URGENT URGENT URGENT URGENT URGENT URGENT

    hi,
    This code is nice but it has big issue.
    My cart is sare to all user.I want one cart for one user not shareable cart.
    Plz help me!

    send code to amity.prasad@gmail.com

    ( Reply )
  65. PG

    BxBEnny December 13th

    Tnx a lot. Why did u use Session? Is it the best way to solve security problem?

    ( Reply )
  66. PG

    Dan Petru December 17th

    Following Kris Crosby modiffications fixes everything

    ( Reply )
  67. PG

    magic December 19th

    Nice and clean
    gj dude

    ( Reply )
  68. PG

    Ami Gill January 11th

    Nice Work…Thanks.

    ( Reply )
  69. PG

    narinder January 14th

    nice coding it solve my problem

    ( Reply )
  70. PG

    george hennessy January 14th

    I downloaded the source codes and pasted them into my shell files and they worked as advertised. I guess I’m learning through reverse engineering. I really like asp.net and C#, I just wish there were tutorials available that are “beginner-friendly” and professionally presented. Sigh…….

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    January 14th