Solved

Doing a shopping cart in ASP.NET

Posted on 2004-08-31
3
530 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

830 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