Click here to Skip to main content
15,891,431 members
Articles / Web Development / ASP.NET

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 423.5K   21.9K   69  
An easy to implement shopping cart for any database.
using System;
using System.Data;
using System.Data.OleDb;

namespace Coder2k
{
	public class Cart
	{
		private string strConnection;
		
		public Cart(string conn)
		{
			strConnection = conn;
		}
		
		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;
		}
		
		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();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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