Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Article

Easy ASP.NET Shopping Cart

Rate me:
Please Sign up or sign in to vote.
2.21/5 (24 votes)
10 Mar 2003CPOL1 min read 422.7K   21.9K   69   28
An easy to implement shopping cart for any database.

Sample Image - shopcart.gif

Introduction

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.

C#
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.

C#
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;
    comCart.Parameters.Add(parmCartID);

    conCart.Open();

    OleDbDataReader result =
      comCart.ExecuteReader(CommandBehavior.CloseConnection);

    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.

C#
        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;
            comItem.Parameters.Add(parmCartID);
            
            OleDbParameter parmProdID = new 
              OleDbParameter("@ProdID", OleDbType.Integer, 4);
            parmProdID.Value = ProdID;
            comItem.Parameters.Add(parmProdID);
            
            OleDbParameter parmQuant = new 
              OleDbParameter("@Quant", OleDbType.Integer, 4);
            parmQuant.Value = Quantity;
            comItem.Parameters.Add(parmQuant);
            
            conItem.Open();
            comItem.ExecuteNonQuery();
            conItem.Close();
        }
        
        public string GetCartID()
        {
            System.Web.HttpContext context = 
                  System.Web.HttpContext.Current;
            
            if (context.Request.Cookies["West_CartID"] != null)
            {
                return context.Request.Cookies["West_CartID"].Value;
            }
            else
            {
                Guid tempGuid = Guid.NewGuid();
                
                context.Response.Cookies["West_CartID"].Value 
                                             = 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.

License

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


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

Comments and Discussions

 
GeneralNeed Assistance Pin
preetshweety2-Apr-07 0:22
preetshweety2-Apr-07 0:22 
QuestionOrder Confirmation Pin
stixoffire20-Feb-07 0:04
stixoffire20-Feb-07 0:04 
AnswerRe: Order Confirmation Pin
Coder2k20-Feb-07 5:20
Coder2k20-Feb-07 5:20 
GeneralRe: Order Confirmation Pin
stixoffire20-Feb-07 8:45
stixoffire20-Feb-07 8:45 
GeneralRe: Order Confirmation Pin
Coder2k20-Feb-07 9:11
Coder2k20-Feb-07 9:11 
GeneralPlease Post Only Complete and Reviewed Project Pin
ntorrisi7-Feb-06 7:47
ntorrisi7-Feb-06 7:47 
QuestionWhat if cookies are disabled? Pin
Anonymous8-Mar-05 18:23
Anonymous8-Mar-05 18:23 
AnswerRe: What if cookies are disabled? Pin
Coder2k9-Mar-05 5:39
Coder2k9-Mar-05 5:39 
GeneralRe: What if cookies are disabled? Pin
Mike (PhilaNJ)9-Apr-05 4:05
Mike (PhilaNJ)9-Apr-05 4:05 
GeneralRe: What if cookies are disabled? Pin
Anonymous23-Oct-05 4:43
Anonymous23-Oct-05 4:43 
GeneralRe: What if cookies are disabled? Pin
Kelsey Thornton8-Dec-07 4:10
Kelsey Thornton8-Dec-07 4:10 
Generali need help Pin
intense010129-Jun-04 19:09
intense010129-Jun-04 19:09 
GeneralRe: i need help Pin
Matt Redmond20-May-09 10:27
Matt Redmond20-May-09 10:27 
QuestionWhere is the SQL needed for this sample? Pin
Chris Allen Wright4-Jun-04 2:20
Chris Allen Wright4-Jun-04 2:20 
AnswerRe: Where is the SQL needed for this sample? Pin
Coder2k21-Jun-04 10:42
Coder2k21-Jun-04 10:42 
GeneralRe: Where is the SQL needed for this sample? Pin
dennym10-Mar-05 14:51
dennym10-Mar-05 14:51 
GeneralRe: Where is the SQL needed for this sample? Pin
Coder2k11-Mar-05 5:09
Coder2k11-Mar-05 5:09 
GeneralRe: Where is the SQL needed for this sample? Pin
sicruise2-Sep-05 4:03
sicruise2-Sep-05 4:03 

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.