Click here to Skip to main content
6,595,854 members and growing! (18,658 online)
Email Password   helpLost your password?
Web Development » User Controls » General     Intermediate License: The Code Project Open License (CPOL)

Easy ASP.NET Shopping Cart

By Coder2k

An easy to implement shopping cart for any database.
C#, Windows, .NET 1.0, ASP.NET, Visual Studio, Dev
Posted:10 Mar 2003
Views:182,542
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 2.34 Rating: 1.83 out of 5
13 votes, 68.4%
1

2
3 votes, 15.8%
3

4
3 votes, 15.8%
5

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.

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;
            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.

        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)

About the Author

Coder2k


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

Other popular User Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
GeneralDoubt Pinmemberkaranrambo12:36 7 Nov '09  
GeneralRe: Doubt PinmemberCoder2k17:15 7 Nov '09  
GeneralAivea Commerce Server Pinmemberaiveacorporation14:42 30 May '09  
GeneralDAAB Pinmembersheemap22:58 23 May '09  
GeneralDAAB Pinmembersheemap22:57 23 May '09  
GeneralMy vote of 1 PinmemberMember 31624429:21 29 Dec '08  
GeneralUsless waste of time AVOID LIKE THE PLAUGE Pinmembersofwreng5:41 1 Jun '07  
GeneralRe: Usless waste of time AVOID _sofwreng_ LIKE THE PLAUGE Pinmemberrimblock4:29 26 Jun '07  
GeneralNeed Assistance Pinmemberpreetshweety1:22 2 Apr '07  
QuestionOrder Confirmation Pinmemberstixoffire1:04 20 Feb '07  
AnswerRe: Order Confirmation PinmemberCoder2k6:20 20 Feb '07  
GeneralRe: Order Confirmation Pinmemberstixoffire9:45 20 Feb '07  
GeneralRe: Order Confirmation PinmemberCoder2k10:11 20 Feb '07  
GeneralPlease Post Only Complete and Reviewed Project Pinmemberntorrisi8:47 7 Feb '06  
GeneralWhat if cookies are disabled? PinsussAnonymous19:23 8 Mar '05  
GeneralRe: What if cookies are disabled? PinmemberCoder2k6:39 9 Mar '05  
GeneralRe: What if cookies are disabled? PinmemberMike (PhilaNJ)5:05 9 Apr '05  
GeneralRe: What if cookies are disabled? PinsussAnonymous5:43 23 Oct '05  
GeneralRe: What if cookies are disabled? PinmemberMember 45956965:10 8 Dec '07  
Generali need help Pinmemberintense010120:09 29 Jun '04  
GeneralRe: i need help PinmemberMatt Redmond11:27 20 May '09  
GeneralWhere is the SQL needed for this sample? PinmemberChris Allen Wright3:20 4 Jun '04  
GeneralRe: Where is the SQL needed for this sample? PinmemberCoder2k11:42 21 Jun '04  
GeneralRe: Where is the SQL needed for this sample? Pinmemberdennym15:51 10 Mar '05  
GeneralRe: Where is the SQL needed for this sample? PinmemberCoder2k6:09 11 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Mar 2003
Editor: Smitha Vijayan
Copyright 2003 by Coder2k
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project