Click here to Skip to main content
13,358,079 members (57,783 online)
Click here to Skip to main content
Add your own
alternative version


69 bookmarked
Posted 10 Mar 2003

Easy ASP.NET Shopping Cart

, 10 Mar 2003
Rate this:
Please Sign up or sign in to vote.
An easy to implement shopping cart for any database.

Sample Image - shopcart.gif


If you take a look at my code, you will see that it is not well documented, but I will try and remedy that with this article. The shopping cart object that I will demonstrate here was written by me for use in an ecommerce web page, as part of a library.

Shopping Cart

Let's get right into the object. The following section will create our object. We pass the connection string from the web page so that it is common.

using System;
using System.Data;
using System.Data.OleDb;

namespace Coder2k
    public class Cart
        private string strConnection;
        public Cart(string conn)
            strConnection = conn;

The following is where our web page will get its cart. We'll demonstrate that later. We use stored procedures so that we can use any database that has an OLE interface and supports stored procedures.

public OleDbDataReader GetCart(string CartID)
    OleDbConnection conCart = new OleDbConnection(strConnection);
    OleDbCommand comCart = new OleDbCommand("qryGetCart", conCart);

    comCart.CommandType = CommandType.StoredProcedure;

    OleDbParameter parmCartID = new
      OleDbParameter("@CartID", OleDbType.VarChar, 50);
    parmCartID.Value = CartID;


    OleDbDataReader result =

    return result;

Now, let's look at the rest of it which is very similar to this. Because of stored procedures, we don't need to know anything about the database in this object, which makes it easier to code.

        public void AddItem(string CartID, int ProdID, int Quantity)
            OleDbConnection conItem = new OleDbConnection(strConnection);
            OleDbCommand comItem = new OleDbCommand("qryAddItem", conItem);
            comItem.CommandType = CommandType.StoredProcedure;
            OleDbParameter parmCartID = new 
              OleDbParameter("@CartID", OleDbType.VarChar, 50);
            parmCartID.Value = CartID;
            OleDbParameter parmProdID = new 
              OleDbParameter("@ProdID", OleDbType.Integer, 4);
            parmProdID.Value = ProdID;
            OleDbParameter parmQuant = new 
              OleDbParameter("@Quant", OleDbType.Integer, 4);
            parmQuant.Value = Quantity;
        public string GetCartID()
            System.Web.HttpContext context = 
            if (context.Request.Cookies["West_CartID"] != null)
                return context.Request.Cookies["West_CartID"].Value;
                Guid tempGuid = Guid.NewGuid();
                                             = tempGuid.ToString();
                return tempGuid.ToString();

Now, the getCartID function is a strange one. It checks to see if the current user has a cart. If they do, it returns that ID, otherwise, it creates a new cart.

Web Pages

I think that the web pages can pretty much speak for themselves if you know ASP web controls. In a later edition of this article, I will explain more on these files, and maybe add some more functionality to the whole thing.


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


About the Author

Software Developer
Canada Canada
I am a developer working with C# and .NET in open source projects.

You may also be interested in...


Comments and Discussions

Praiselsfjlsfkdsfjdssdlkfjdsf Pin
Member 95633204-Aug-17 22:33
memberMember 95633204-Aug-17 22:33 
Praiselsfjlsfkdsfjds Pin
Member 95633204-Aug-17 22:33
memberMember 95633204-Aug-17 22:33 
GeneralDoubt Pin
karanrambo7-Nov-09 12:36
memberkaranrambo7-Nov-09 12:36 
GeneralRe: Doubt Pin
Coder2k7-Nov-09 17:15
memberCoder2k7-Nov-09 17:15 
GeneralAivea Commerce Server Pin
aiveacorporation30-May-09 14:42
memberaiveacorporation30-May-09 14:42 
GeneralDAAB Pin
sheemap23-May-09 22:58
membersheemap23-May-09 22:58 
GeneralDAAB Pin
sheemap23-May-09 22:57
membersheemap23-May-09 22:57 
GeneralMy vote of 1 Pin
Member 316244229-Dec-08 9:21
memberMember 316244229-Dec-08 9:21 
don't have meening.
GeneralUsless waste of time AVOID LIKE THE PLAUGE Pin
sofwreng1-Jun-07 5:41
membersofwreng1-Jun-07 5:41 
GeneralRe: Usless waste of time AVOID _sofwreng_ LIKE THE PLAUGE Pin
rimblock26-Jun-07 4:29
memberrimblock26-Jun-07 4:29 
GeneralNeed Assistance Pin
preetshweety2-Apr-07 1:22
memberpreetshweety2-Apr-07 1:22 
QuestionOrder Confirmation Pin
stixoffire20-Feb-07 1:04
memberstixoffire20-Feb-07 1:04 
AnswerRe: Order Confirmation Pin
Coder2k20-Feb-07 6:20
memberCoder2k20-Feb-07 6:20 
GeneralRe: Order Confirmation Pin
stixoffire20-Feb-07 9:45
memberstixoffire20-Feb-07 9:45 
GeneralRe: Order Confirmation Pin
Coder2k20-Feb-07 10:11
memberCoder2k20-Feb-07 10:11 
GeneralPlease Post Only Complete and Reviewed Project Pin
ntorrisi7-Feb-06 8:47
memberntorrisi7-Feb-06 8:47 
QuestionWhat if cookies are disabled? Pin
Anonymous8-Mar-05 19:23
sussAnonymous8-Mar-05 19:23 
AnswerRe: What if cookies are disabled? Pin
Coder2k9-Mar-05 6:39
memberCoder2k9-Mar-05 6:39 
GeneralRe: What if cookies are disabled? Pin
Mike (PhilaNJ)9-Apr-05 5:05
memberMike (PhilaNJ)9-Apr-05 5:05 
GeneralRe: What if cookies are disabled? Pin
Anonymous23-Oct-05 5:43
sussAnonymous23-Oct-05 5:43 
GeneralRe: What if cookies are disabled? Pin
Member 45956968-Dec-07 5:10
memberMember 45956968-Dec-07 5:10 
Generali need help Pin
intense010129-Jun-04 20:09
memberintense010129-Jun-04 20:09 
GeneralRe: i need help Pin
Matt Redmond20-May-09 11:27
memberMatt Redmond20-May-09 11:27 
QuestionWhere is the SQL needed for this sample? Pin
Chris Allen Wright4-Jun-04 3:20
memberChris Allen Wright4-Jun-04 3:20 
AnswerRe: Where is the SQL needed for this sample? Pin
Coder2k21-Jun-04 11:42
memberCoder2k21-Jun-04 11:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 11 Mar 2003
Article Copyright 2003 by Coder2k
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid