i am trying to add items to cart which are stored in session but the session values are overwritten when i add a new product to the cart.
private List<int> newCart = new List<int>();
protected void dlstCartItems_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
var arg = e.CommandArgument;
DropDownList ddlList = e.Item.FindControl("ddlAvailableSizes"+e.CommandArgument) as DropDownList;
int currentItemID = int.Parse(this.dlstCartItems.DataKeys[e.Item.ItemIndex].ToString());
if (ddlList.SelectedIndex == 0)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Please Select Size');", true);
}
else
{
newCart.Add(Convert.ToInt32(e.CommandArgument));
Session["Cart"] = newCart;
if (Session["Cart"] != null)
{
int ct = ((List<int>)Session["Cart"]).Count;
lblCartMessage.Text = Convert.ToString(ct)+" "+"Product";
}
}
}
}
my problem is when ever i add a new product the old product is no more there in the list and the product count is always 1.
The list in which u are adding should be defined at a class level. Whenever you are adding new product , private List newCart = new List(); should not be executed otherwise it will make the count zero after addition you would only be left with the latest addition.