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?
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.
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
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>();
}
}
Thanks & Regards
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
markbpriv
Member
25 Points
143 Posts
Database table or Dataset for site shopping cart?
Nov 29, 2012 12:08 PM|LINK
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
DarrellNorto...
All-Star
86665 Points
9634 Posts
Moderator
MVP
Re: Database table or Dataset for site shopping cart?
Nov 29, 2012 07:00 PM|LINK
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.
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
asteranup
All-Star
30184 Points
4906 Posts
Re: Database table or Dataset for site shopping cart?
Nov 30, 2012 03:20 AM|LINK
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>(); } }Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog