Solved

Doing a shopping cart in ASP.NET

Posted on 2004-08-31
3
531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp Google Map 2 91
Wordpress Only run code if on a certain page 11 46
Bootstrap list items overlap 3 55
WordPress 8 69
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Businesses who process credit card payments have to adhere to PCI Compliance standards. Here’s why that’s important.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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