Solved

Doing a shopping cart in ASP.NET

Posted on 2004-08-31
3
528 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
ID: 11940354
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
ID: 11940661
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
ID: 11943340
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
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.

809 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