Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples VB.NET
 

Sample Cart


Code ResourceAuthor: Biswabed Dash  

Difficulty Level:

Published: 10th Nov 2006   Read: 828 times  

Filed in: VB.NET
Add Comment


 


This is a simple class written in C# which enables you to add items chose by a person store. and retrieve when required. To use the class create an object of the class and use it's various functions to achieve the desired result.

 


Sample Code
  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.SessionState;
  10. using System.Web.Caching;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.WebControls.WebParts;
  13. using System.Web.UI.HtmlControls;
  14.  
  15. public class SimpleCart:System.Web.UI.Page
  16. {
  17.     private int _intTotalItem;
  18.     private double _dblNetPrice;
  19.     private System.Data.DataTable _dtCart;
  20.  
  21.  
  22.     ///<summary>
  23.     ///Author           :  Biswabed Dash
  24.     ///Name             :  TotalItem
  25.     ///Return Value     :  int
  26.     ///Purpose          :  This field exposes Total number of items added by customer to the cart.
  27.     ///Created Date     :  25/10/2006      
  28.     /// </summary>
  29.     public int TotalItem
  30.     {
  31.         get
  32.         {
  33.             return _intTotalItem;
  34.         }
  35.         set
  36.         {
  37.             _intTotalItem = value;
  38.         }
  39.     }
  40.  
  41.     ///<summary>
  42.     ///Author           :  Biswabed Dash
  43.     ///Name             :  NetPrice
  44.     ///Return Value     :  double
  45.     ///Purpose          :  This field exposes the Net Order Price  for the customer.
  46.     ///Created Date     :  25/10/2006      
  47.     /// </summary>
  48.     public double NetPrice
  49.     {
  50.         get
  51.         {
  52.             return _dblNetPrice;
  53.         }
  54.         set
  55.         {
  56.             _dblNetPrice = value;
  57.         }
  58.     }
  59.  
  60.     ///<summary>
  61.     ///Author           :  Biswabed Dash
  62.     ///Name             :  AddedProductList
  63.     ///Return Value     :  DataTable
  64.     ///Purpose          :  This field exposes the product list with details added by the customer to the cart.
  65.     ///Created Date     :  25/10/2006      
  66.     /// </summary>
  67.  
  68.     public System.Data.DataTable AddedProductList
  69.     {
  70.         get
  71.         {
  72.             return _dtCart;
  73.         }
  74.         set
  75.         {
  76.             _dtCart = value;
  77.         }
  78.     }
  79.    
  80.  
  81.     ///<summary>
  82.     ///Author           :  Biswabed Dash
  83.     ///Name             :  MakeCart
  84.     ///Return Value     :  void
  85.     ///Purpose          :  MakeCart Function build the the cart stucture.
  86.     ///Created Date     :  25/10/2006      
  87.     ///</summary>
  88.  
  89.     private void MakeCart()
  90.     {
  91.         TotalItem = 0;
  92.         NetPrice = 0.0;
  93.         _dtCart = new System.Data.DataTable();
  94.         _dtCart.Columns.Add(new DataColumn("ProductCode", typeof(string)));
  95.         _dtCart.Columns.Add(new DataColumn("ProductName", typeof(string)));
  96.         _dtCart.Columns.Add(new DataColumn("Quantity", typeof(int)));
  97.         _dtCart.Columns.Add(new DataColumn("UnitPrice", typeof(Double)));
  98.         _dtCart.Columns.Add(new DataColumn("Total", typeof(Double)));
  99.     }
  100.  
  101.     ///<summary>
  102.     ///Author           :  Biswabed Dash
  103.     ///Name             :  AddToCart
  104.     ///Return Value     :  void
  105.     ///Purpose          :  AddToCart Function enables Customer to add a Product to the Cart.
  106.     ///Created Date     :  25/10/2006      
  107.     ///</summary>
  108.     ///<param name="pProductCode">Product Code that customer wants to add to the cart</param>
  109.     ///<param name="pProductName">Product Name that customer wants to add to the cart</param>
  110.     ///<param name="pQuantity">Numbers of items customer wants for the selected product</param>
  111.     ///<param name="pUnitPrice">Unit Price for the selected Product </param>
  112.    
  113.     public void AddToCart(string pProductCode, string pProductName, int pQuantity, double pUnitPrice)
  114.     {
  115.         if (Session["Cart"] != null)
  116.         {
  117.            
  118.            
  119.  
  120.             SimpleCart obj = (SimpleCart)Session["Cart"];
  121.             DataRow[] rows = obj._dtCart.Select("ProductCode='" + pProductCode + "'");
  122.             if (rows.Length == 0)
  123.             {
  124.                 System.Data.DataRow objDR = obj._dtCart.NewRow();
  125.                 objDR["ProductCode"] = pProductCode;
  126.                 objDR["ProductName"] = pProductName;
  127.                 objDR["Quantity"] = pQuantity;
  128.                 objDR["UnitPrice"] = pUnitPrice;
  129.                 objDR["Total"] = pQuantity * pUnitPrice;
  130.                 obj._dtCart.Rows.Add(objDR);
  131.                
  132.                 obj.TotalItem += (int)objDR["Quantity"];
  133.                 obj.NetPrice += (double)objDR["Total"];
  134.             }
  135.             else
  136.             {
  137.                 DataRow row = (DataRow)rows[0];
  138.                 row["Quantity"] = Convert.ToInt32(row["Quantity"])+ pQuantity;
  139.                 row["Total"] = Convert.ToInt32(row["Quantity"]) * Convert.ToDouble( row["UnitPrice"]);
  140.                 row.AcceptChanges();
  141.                 obj.TotalItem += pQuantity;
  142.                 obj.NetPrice += pQuantity * pUnitPrice;
  143.             }
  144.            
  145.             Session["Cart"] = obj;
  146.  
  147.         }
  148.         else
  149.         {
  150.             MakeCart();
  151.             System.Data.DataRow objDR = _dtCart.NewRow();
  152.             objDR["ProductCode"] = pProductCode;
  153.             objDR["ProductName"] = pProductName;
  154.             objDR["Quantity"] = pQuantity;
  155.             objDR["UnitPrice"] = pUnitPrice;
  156.             objDR["Total"] = pQuantity * pUnitPrice;
  157.             _dtCart.Rows.Add(objDR);
  158.            
  159.             TotalItem += (int)objDR["Quantity"];
  160.             NetPrice += (double)objDR["Total"];
  161.            
  162.             Session["Cart"] = this;
  163.         }
  164.  
  165.     }
  166.  
  167.     ///<summary>
  168.     ///Author           :  Biswabed Dash
  169.     ///Name             :  DeleteFromCart
  170.     ///Return Value     :  void
  171.     ///Purpose          :  DeleteFromCart Function removes a Product which Product Code provideed as Parameter from the Cart.
  172.     ///Created Date     :  25/10/2006      
  173.     ///</summary>
  174.     ///<param name="pProductCode">Product Code that customer wants to remove from the cart</param>
  175.    
  176.     public void DeleteFromCart(string pProductCode)
  177.     {
  178.         if (Session["Cart"] != null)
  179.         {
  180.             SimpleCartobj = (SimpleCart)Session["Cart"];
  181.             DataRow[] rows = obj._dtCart.Select("ProductCode='" + pProductCode + "'");
  182.            
  183.                 DataRow row = (DataRow)rows[0];
  184.                 obj.TotalItem -= (int)row["Quantity"];
  185.                 obj.NetPrice -= (double)row["Total"];
  186.                 row.Delete();
  187.            
  188.  
  189.             Session["Cart"] = obj;
  190.         }
  191.     }
  192.  
  193.     ///<summary>
  194.     ///Author           :  Biswabed Dash
  195.     ///Name             :  UpdateCart
  196.     ///Return Value     :  void
  197.     ///Purpose          :  DeleteFromCart Function removes a Product which Product Code provideed as Parameter from the Cart.
  198.     ///Created Date     :  25/10/2006      
  199.     ///</summary>
  200.     ///<param name="pProductCode">Product Code that customer wants to remove from the cart</param>
  201.     ///<param name="pQuantity">Quantity need to be Updated by the customer for the Product whose code Passed as first Parameter.</param>
  202.    
  203.     public void UpdateCart(string pProductCode,int pQuantity)
  204.     {
  205.         SimpleCart obj = (SimpleCart)Session["Cart"];
  206.         obj.NetPrice = 0.0;
  207.         obj.TotalItem = 0;
  208.         for (int rowcnt = 0; rowcnt <= obj._dtCart.Rows.Count - 1; rowcnt++)
  209.         {
  210.             string str = (string)obj._dtCart.Rows[rowcnt]["ProductCode"];
  211.             if (str == pProductCode)
  212.             {
  213.                 obj._dtCart.Rows[rowcnt]["Quantity"] = pQuantity;
  214.                 obj._dtCart.Rows[rowcnt]["Total"] = pQuantity * Convert.ToDouble(obj._dtCart.Rows[rowcnt]["UnitPrice"]);
  215.                 obj._dtCart.Rows[rowcnt].AcceptChanges();
  216.             }
  217.             obj.TotalItem += (int)obj._dtCart.Rows[rowcnt]["Quantity"];
  218.             obj.NetPrice += (double)obj._dtCart.Rows[rowcnt]["Total"];
  219.  
  220.         }
  221.  
  222.         Session["Cart"] = obj;
  223.        
  224.     }
  225. }
  226.  
Copyright GeekInterview.com


Next Article: Birth Number from Date - Numerology


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. Lokesh5943
2. supersubra2506
3. Lokesh M1558
4. Biswabed Dash829

Active Coders

# Coder NameCodes
1. supersubra1
2. Biswabed Dash1
3. Lokesh1
4. Lokesh M1

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape