[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Doing a shopping cart in ASP.NET

Posted on 2004-08-31
3
Medium Priority
?
542 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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