Solved

Doing a shopping cart in ASP.NET

Posted on 2004-08-31
3
522 Views
Last Modified: 2013-11-29
Hi,

I have question about how to go about constructing a shopping cart in ASP.NET.  I understand c# and asp.net pretty well so my question is not one of how to do it it but one of design.

Is the best way to have the shopping cart this:

1)Create a DataTable and add each cart item to this datatable.
2)This DataTable is then stored in a Session variable and therefore passed around and can be seen on every page?

Is this good or bad?  

All help greatly appreciated as always!

Mike
0
Comment
Question by:hydev
3 Comments
 
LVL 1

Accepted Solution

by:
Cart_man earned 500 total points
Comment Utility
I've done it like that and it works great.

What you need to do is creating a class that deals with all the main functions of that shopping cart, such as AddProduct, DeleteProduct, EmptyCart, etc.

Here is a class you can use:

using System;
using System.Data;
using System.Web;
using DAL;     // my own class for running queries against a database

namespace WebApp
{
      /// <summary>
      /// Summary description for Cart.
      /// </summary>
      public class Cart
      {
            public Cart()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public static void AddToCart(int productId, int quant)
            {
                  DataTable dt = GetCart();
                  
                  DataRow rowFound = dt.Rows.Find(productId);
                  if (rowFound == null)
                  {
                        DataRow dr = dt.NewRow();
                        dr["product_id"]=productId;
                        dr["product_quant"]=quant;
                        dt.Rows.Add(dr);
                  }
                  else
                  {
                        rowFound["product_quant"]=Convert.ToInt32(rowFound["product_quant"]) + quant;
                  }

                  for (int i=0; i<dt.Rows.Count; i++)
                  {
                        string query = "select product_title, product_price from t_products where product_id=" + dt.Rows[i]["product_id"]; // this returns relevant data such as the product name and price to store in your datatable.
                        DataTable dtRes = DataLayer.GetDataTable(query, DataLayer.SqlConn);
                        dt.Rows[i]["product_name"]=dtRes.Rows[0]["product_title"];
                        dt.Rows[i]["product_price"]=dtRes.Rows[0]["product_price"];
                  }
                  HttpContext.Current.Session["shoppingCart"]=dt;
            }

            public static DataTable GetCart()  // returns the shopping cart as a datatable or creates a shopping cart if none is present.
            {
                  if (HttpContext.Current.Session["shoppingCart"]!=null)
                  {
                        DataTable dt = (DataTable)HttpContext.Current.Session["shoppingCart"];
                        return dt;
                  }
                  else
                  {
                        DataTable dt = new DataTable();
                        dt.PrimaryKey=new DataColumn[] {dt.Columns.Add("product_id")};
                        dt.Columns.Add("product_quant");
                        dt.Columns.Add("product_name");
                        dt.Columns.Add("product_price");
                        return dt;
                  }
            }

            public static bool IsInCart(int productId)   // returns whether a certain product is in the shopping cart
            {
                  DataTable dt = GetCart();
                  DataRow dr = dt.Rows.Find(productId);
                  return (dr!=null);
            }

            public static int GetTotalAmmount()   // returns the total ammount based on the price and quantity of each item
            {
                  DataTable dt = GetCart();
                  int total = 0;

                  for (int i=0; i<dt.Rows.Count; i++)
                  {
                        string query = "select product_price * " + dt.Rows[i]["product_quant"] + " as total from t_products where product_id=" + dt.Rows[i]["product_id"];
                        total+=Convert.ToInt32(DataLayer.GetDataTable(query, DataLayer.SqlConn).Rows[0]["total"]);
                  }

                  return total;
            }

            public static void UpdateCart(int productId, int productQuant)   // Modifies the quantity of a product in the cart
            {
                  DataTable dt = GetCart();
                  DataRow dr = dt.Rows.Find(productId);
                  dr["product_quant"]=productQuant;
            }

            public static void DeleteItem(int productId)   // deletes a product from the cart
            {
                  DataTable dt = GetCart();
                  DataRow dr = dt.Rows.Find(productId);
                  dr.Delete();
            }

            public static void IssueOrder()
            {

                                                // Write your own code to store the order in the database, send the confirmation to the customer, etc.


            }
      }
}
0
 
LVL 2

Expert Comment

by:anilmane
Comment Utility
I thinks your question will get solve if you refer
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdasamppet.asp

Thanks
Anil Mane
0
 
LVL 6

Expert Comment

by:Eric
Comment Utility
take a look at the open source of a cart called MetaCart.  that should help ya out a bit..


http://metalinks.com/metacart/default.asp

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope‚Ķ
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now