locked
Database table or Dataset for site shopping cart? RRS feed

  • Question

  • User1764424651 posted

    Hi,

    Not sure if this is the write forum to post this in but here goes. I hope some one can help me.

    I am developing an ASP.net web site and it has a shopping cart.

    Currently I am storing the items of the shopping cart in an SQL table.
    Once the users has bought the items I copy the items they bought to an order table, and then remove their data from the shopping cart table.

    The problem I have with this approach is if users don't buy the items I'm left with lots of data in the shopping cart table, and therefore will need to create some SQL server job to empty it periodically.

    I am now considering using a temporrary dataset (storing it a session) but by doing this the users will loose the data if they exit the site.

    Form a performance and server overhead point of view what approach is best?

    Cheers

    Mark

    Thursday, November 29, 2012 8:08 AM

All replies

  • User-837620913 posted

    Write the data to the database. Storing items in session for each user involves more server overhead. For a select that returns a single row (selecting the cart for a user from a database table) will usually be a very fast operation.

    You could add a nightly SQL Server task to clear out shopping carts that are older than a certain number of days.

    Thursday, November 29, 2012 3:00 PM
  • User348806598 posted

    Hi,

    I think you can maintain full cart in session and once during checkout you can save the session cart data to database. Check the following sample code. You can modify your cart accordingly-

        public partial class Show_dialog_on_check_changed : System.Web.UI.Page
        {
            public ShoppingCart SessionCart {
                get {
                    return (ShoppingCart)Session["Cart"];
                }
                set {
                    Session["Cart"] = value;
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                
                ShoppingCart cart = new ShoppingCart();
                cart.Items.Add(new CartItem() { });
    
                //set session cart
                SessionCart = cart;
    
                //get cart from session
                ShoppingCart cart1 = SessionCart;
            }
        }
        public class CartItem 
        {
            public int ItemID { get; set; }
            public string ItemName { get; set; }
            public int Quantity { get; set; }
            public float Price { get; set; }
            public float Total { get; set; }
        }
        public class ShoppingCart {
            public List<CartItem> Items { get; set; }
            public ShoppingCart()
            {
                Items = new List<CartItem>();
            }
        }
    Thursday, November 29, 2012 11:20 PM