Click here to Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDoubt
karanrambo
12:36 7 Nov '09  
How to remove the record if the user clicks on the remove button . Also it should delete only that record from the dagrid as well.
GeneralRe: Doubt
Coder2k
17:15 7 Nov '09  
I wrote this 6 years ago and no longer have any of the code. There are better solutions available. Sorry.
GeneralAivea Commerce Server
aiveacorporation
14:42 30 May '09  
Aivea Commerce Server http://www.aivea.com/aivea-commerce-server.htm[^] is a eCommerce product based on ASP.NET 3.5, WCF, WF, LINQ and SQL Server 2008Thumbs Up
GeneralDAAB
sheemap
22:58 23 May '09  
hiiiii to all
please tell reason when i configuring the enterprise libray,right click on connection string there is option oracle not sql server,please answer how to cofiguring it?

rizvan sivally

GeneralDAAB
sheemap
22:57 23 May '09  
hiiiii to all
please tell reason when i configuring the enterprise libray,right click on connection there is option oracle not sql server,please answer how to cofiguring it?

rizvan sivally

GeneralMy vote of 1
Member 3162442
9:21 29 Dec '08  
don't have meening.
GeneralUsless waste of time AVOID LIKE THE PLAUGE
sofwreng
5:41 1 Jun '07  
Waste of time, usless pile of garbage. Why are they allowing these posts here anyway, I am losing all interest in this lamo site because of these futile attempts and useless code posts.
Get a grip people.

was a user, tired of my time being wasted
GeneralRe: Usless waste of time AVOID _sofwreng_ LIKE THE PLAUGE
rimblock
4:29 26 Jun '07  
sofwreng wrote:
Waste of time, usless pile of garbage. Why are they allowing these posts here anyway, I am losing all interest in this lamo site because of these futile attempts and useless code posts.
Get a grip people.

was a user, tired of my time being wasted

Sounds like you're going to have to use your brain then, instead of relying on other people for a change. Anyone who expects to find a complete solution to their problem on this website is basically a thief anyway.

Why dont you do something useful like producing a better article or alternatively just get lost since this 'lamo site' is made lamo by non-contributary and critical members, like you.

Still a user, tired of valuable atoms being wasted on "people" like you.

Anyone agree?!

GeneralNeed Assistance
preetshweety
1:22 2 Apr '07  
Plz tell me if I can directly embbed these codes in my webpage ? And see the results? or what i have to do to see actually wat thses codes do?

Waiting for ur response.
QuestionOrder Confirmation
stixoffire
1:04 20 Feb '07  
There is no Order Confirmation with this - how would you create a printable receipt on the fly and email to the recipient ..
AnswerRe: Order Confirmation
Coder2k
6:20 20 Feb '07  
I wasn't intending this to be a complete shopping cart implementation. It was meant more as a framework or launching pad for those who didn't know where to start.
GeneralRe: Order Confirmation
stixoffire
9:45 20 Feb '07  
Ok , I understand. I was hoping to find a simple solution to the problem where I could create the document in memory , I know how to do it by creating the file (of course the layout is the hardway I am sure.) I was thinking of using xslt and transforming an xml document - then to html but my experience level with xslt, is limited - I have only read articles - some of the concepts are simple - but doing that in memory on a "virtual" xml document.
The reason I do not want file based is for a number of reasons - the speed of file access, the filenaming I would then need to implement (as multiple customers could be ordering at the same time). I would rather use the speed of memory to whip it out and be done.

Could you give me your thoughts on what you feel is the best approach to do this ?
GeneralRe: Order Confirmation
Coder2k
10:11 20 Feb '07  
Doing it in memory is probably the fastest way of doing it. I would suggest sending a formatted text email. Some email applications block html emails due to their ability to load malware onto a computer.
GeneralPlease Post Only Complete and Reviewed Project
ntorrisi
8:47 7 Feb '06  
The Project simple, fine and helpful for simple shopping cart projects.
But I think also that any project, simple or complexBig Grin , have to be reviewed before
posting. This project is not reviewed...sorry.

Nunzio
GeneralWhat if cookies are disabled?
Anonymous
19:23 8 Mar '05  
It seems like your code relies on cookies to store your cartID. What if the user has disabled cookies?

Thanks.
GeneralRe: What if cookies are disabled?
Coder2k
6:39 9 Mar '05  
Unfortunately they will be unable to use this cart. I don't think there is any other way to store the id. If you can think of one let me know and I will try and implement it.
GeneralRe: What if cookies are disabled?
Mike (PhilaNJ)
5:05 9 Apr '05  
You could use the session object or query string to preserve the id temporarily and cookies to save the id for return visits. Since sessions use temporary cookies which may be rejected, this isn't perfect either. You could check for the state of the browser, try a permanent cookie, if not accepted, try a temporary, if not accepted, use the query string to preserve the id.

Thanks for the article. I'll look forward to trying your code.

Mike
PhilaNJ.com
GeneralRe: What if cookies are disabled?
Anonymous
5:43 23 Oct '05  
Since you are using SQL Server, then you can use it to manage your cookies, that way it doesn't depend on the client browser settings. You have to modify your webconfig file to do this, it's pretty simple. You dont need to write any code.
GeneralRe: What if cookies are disabled?
Member 4595696
5:10 8 Dec '07  
cookies are probably a better place to store the cart than session variables. My erason? session variables take up resources on your server. If your site gets *really* popular, then the server will be spending all its time and resources looking after session variables - this isn't very desirable...
Generali need help
intense0101
20:09 29 Jun '04  
hi

i can see that you are a very skillful programmer. can i have your email so i can email you. i got some problem that can't be discussed here. of course it is a programming related problem. thanks for your attention.Confused Confused
GeneralRe: i need help
Matt Redmond
11:27 20 May '09  
Bwaaaaaaahahahaha! Very skillful programmer? Because he created a cart by saving an item and quantity in a database? Are you kidding?
GeneralWhere is the SQL needed for this sample?
Chris Allen Wright
3:20 4 Jun '04  
That code doesn't do you much good if you don't have a schema for what the database should look like!

--Baffled
GeneralRe: Where is the SQL needed for this sample?
Coder2k
11:42 21 Jun '04  
Sorry I thought I had included an example access db. If I did it correctly though you just need to create an access db with those queries and it should work. I will try and create an example soon.
GeneralRe: Where is the SQL needed for this sample?
dennym
15:51 10 Mar '05  
Could you please include the assess db?
GeneralRe: Where is the SQL needed for this sample?
Coder2k
6:09 11 Mar '05  
Unfortunately at this time I no longer have the database. It really isn't that tough to look at the code and create something that will work. If you need help let me know. I will probably be writing a more advanced cart for this site in the future.


Last Updated 11 Mar 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010