locked
remove session item RRS feed

  • Question

  • User788760882 posted

    I have a simple cart that a user clicks a button and it adds to a session that is then shown in a gridview on the cart page as shown in the page behind code below, now firstly how would I go about removing single items from the list if the user decided he did not want them .

    add to cart code.

    protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
               var id=Int32.Parse(FormView1.DataKey.Value.ToString());
               if(Session["CartItems"]==null)
               {
                   Session["CartItems"] = new List<int>();
               }
                List<int> products = (List<int>) Session["CartItems"];
                if(!products.Contains(id))
                {
                    products.Add(id);
                }
                Session["CartItems"] = products;
            }
        }
    cart code
    protected void BindData()
        {
            if (Session["CartItems"] == null)
            {
                Session["CartItems"] = new List<int>();
            }
            List<int> products = (List<int>)Session["CartItems"];
    
            string sqlCommand ="SELECT [ProductID] , [ProductName], [ProductPrice] FROM [Products] WHERE        [ProductID] in ({0})";
            string ids = string.Join(",", products);
            sqlCommand = string.Format(sqlCommand, ids);
            if(products.Count>0)
            {
                SqlDataSource1.SelectCommand = sqlCommand;
                SqlDataSource1.DataBind();
                gvShoppingCart.DataSourceID = "SqlDataSource1";
                SqlDataSource1.DataBind();
            }
        }




    Saturday, December 8, 2012 12:19 PM

Answers

  • User-2052725427 posted

    Hi,

    When you are clicking on delete button of particular item, then pass the ProductID of that item to your delete query .


    In GridView delete event catch the product id and run below statement.
    products
    .Remove(x => x == Id);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 8, 2012 4:55 PM