SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast theburlster's Avatar
    Join Date
    Jul 2007
    Location
    South Coast of England
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Shopping Basket Basics

    Good Morning,

    Urmmm, I am designing an E-commerce system using ASP.NET 2.0 with Visual Web Developer Express 2005. I have never done anything like this before and am finding it all a bit intimidating but have spent hours researching and learning to get it together.

    Occassionally however, I find myself able to find a thousand tutorials on something but no-one to really put in simple terms where to start. I need to be able to compile a shopping list as a user browses my shop, but how? I know it has something to do with sessions, but for speed perhaps I could not use a temporary table in a database?

    What would you recommend I pass back and forward as a user browses? An array with the sales codes of the products they've purchased? How do I pass this array?

    I'm very keen to learn and if there's a basic concept I'm not getting here and you know of a good 'from beginner' tutorial, that would be most helpful!

    Thank you in advance,

    John
    The Burlster of www.burlster.com fame

  2. #2
    SitePoint Enthusiast theburlster's Avatar
    Join Date
    Jul 2007
    Location
    South Coast of England
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I just found this on this very site, so I shall give it a scan. But any advice will still be appreciated

    http://www.sitepoint.com/article/net...art-datatables
    The Burlster of www.burlster.com fame

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    That article is honestly a bit dated, so it might not be the best guide.

    I think there is an E-Commerce site starter kit on www.asp.net that is a very good place to start. It is better than a tutorial--it is a learning and breathing application.

  4. #4
    SitePoint Enthusiast theburlster's Avatar
    Join Date
    Jul 2007
    Location
    South Coast of England
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update

    Hmmm, well I couldn't find much up there to be honest, or at least as good and clear as the aforementioned article though I appreciate it is obviously quite dated. The site you recommended did however have some great video tutorials for free on other parts of ASP.NET so I am grateful for your pointing it out.

    Are the principals used in the tutorial I mentioned still valid? Could you tell me what part of it is now outdated so I could go ahead and research those individual bits but use the basics of the tutorial to go from?

    Thanks a lot for your help,

    John
    The Burlster of www.burlster.com fame

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Marysville, CA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm currently in the same predicament. however, I do know that there is a pretty good Shopping Cart example in the Murach ASPNET 2.0 book. You could always download the code for free and work through it...

    http://www.murach.com/downloads/ugcs.htm

  6. #6
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basic Shopping Basket Logic...

    The DataTable is a great place to store items in a shopping basket, because you can actually put the whole DataTable into a session, and fetch it again as a DataTable.

    Remember to create a global.asax file, and initiate the session["basket"] from there.

    First off, you need a Class, to handle all the funcitons of your basket.

    The class only need a few basic methods, such as:
    • Private CreateBasket
    • Private SaveBasket
    • Private LoadBasket
    • Public AddItem
    • Public EditItem
    • Public BasketTotal

    Ofcourse you can always extend it with more custom methods if needed.

    CreateBasket:
    This method generates a DataTable, with the needed columns.

    SaveBasket:
    This method puts the DataTable into the session array.

    LoadBasket:
    This method takes the DataTable from the session, and places it in the basket DataTable

    AddItem:
    This method is called from the webpage, and is used to add a given item to the basket.
    It should first check if the current item already exists in the basket.
    If it already exists, then update instead of add.

    EditItem:
    This method is also called from the webpage, and is used to update the basket before an order is completed.

    This is an example of a Basket, it have a few extra methods, such as "DeleteItem" and "EmptyBasket" (sorry about the lack of comments...):
    Code ASP:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
     
    public class ShopBasket
    {
    	private DataTable _dtBasket;
     
    	public void AddItemToBasket(int item_id, string item_no, string item_name, float item_price, int item_qty)
    	{
    		bool addItem = true;
    		foreach (DataRow row in _dtBasket.Rows)
    		{
    			if (Convert.ToInt32(row["item_id"]) == item_id)
    			{
    				addItem = false;
    				int newItemQty = Convert.ToInt32(row["item_qty"]) + item_qty;
    				if (newItemQty < 1)
    				{
    					DeleteItemFromBasket(item_id);
    					break;
    				}
    				else
    				{
    					row["item_qty"] = newItemQty;
    					row["item_total"] = item_price * newItemQty;
    					row.AcceptChanges();
    					_SaveBasket();
    				}
    			}
    		}
     
    		if (addItem)
    		{
    			DataRow row = _dtBasket.NewRow();
    			row["item_id"] = item_id;
    			row["item_no"] = item_no;
    			row["item_name"] = item_name;
    			row["item_price"] = item_price;
    			row["item_qty"] = item_qty;
    			row["item_total"] = item_price * item_qty;
    			_dtBasket.Rows.Add(row);
    			_SaveBasket();
    		}
    	}
     
    	public void EditItemInBasket(int item_id, float item_price, int item_qty)
    	{
    		foreach (DataRow row in _dtBasket.Rows)
    		{
    			if (Convert.ToInt32(row["item_id"]) == item_id)
    			{
    				int newItemQty = Convert.ToInt32(row["item_qty"]) + item_qty;
    				float newItemPrice = Convert.ToSingle(row["item_price"]) + (item_qty * item_price);
    				if (newItemQty < 1)
    				{
    					DeleteItemFromBasket(item_id);
    					break;
    				}
    				else
    				{
    					row["item_qty"] = Convert.ToInt32(row["item_qty"]) + item_qty;
    					row["item_price"] = Convert.ToSingle(row["item_price"]) + (item_qty * item_price);
    					row.AcceptChanges();
    					_SaveBasket();
    				}
    			}
    		}
    	}
     
    	public void DeleteItemFromBasket(int item_id)
    	{
    		foreach (DataRow row in _dtBasket.Rows)
    		{
    			if (Convert.ToInt32(row["item_id"]) == item_id)
    			{
    				row.Delete();
    				_SaveBasket();
    			}
    		}
    	}
     
     
    	private void _CreateBasket()
    	{
    		_dtBasket = new DataTable();
    		_dtBasket.Columns.Add("item_id", typeof(int));
    		_dtBasket.Columns.Add("item_no", typeof(string));
    		_dtBasket.Columns.Add("item_name", typeof(string));
    		_dtBasket.Columns.Add("item_price", typeof(float));
    		_dtBasket.Columns.Add("item_total", typeof(float));
    		_dtBasket.Columns.Add("item_qty", typeof(int));
    	}
     
    	private void _SaveBasket()
    	{
    		HttpContext.Current.Session["basket"] = _dtBasket;
    	}
     
    	private void _LoadBasket()
    	{
    		_dtBasket = (DataTable)HttpContext.Current.Session["basket"];
    	}
     
    	private void _EmptyBasket()
    	{
    		_dtBasket.Clear();
    		HttpContext.Current.Session.Remove("basket");
    	}
     
    	public ShopBasket()
    	{
    		if (HttpContext.Current.Session["basket"] != null)
    		{
    			_LoadBasket();
    		}
    		else
    		{
    			_CreateBasket();
    			_SaveBasket();
    		}
    	}
    }

    Ontop of this class, I can recommend creating a UserControl to display the Basket Summary, and include the "Goto Basket" button and the "CheckOut" button.

    Hope it helps, atleast with some of the concepts behind a Basket.
    zalucius

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Why back it with a DataTable? You could get to the same place using some custom objects and avoid alot of the casting, boxing and other hoo ha.

  8. #8
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    Why back it with a DataTable? You could get to the same place using some custom objects and avoid alot of the casting, boxing and other hoo ha.
    Well, the DataTable is easy to work with, adding rows and columns and such, plus it also works great as a DataSource for a GridView/DataGrid when you want to display the basket..

    but ofcourse, there are other options, this is just an example on one of those solutions... fell free to supply alternatives.
    zalucius


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •