Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The quantity of products incorrect after update the product.
Ex:select Product 001->quantity:1
Product 002->add 002,quantity:1
Product 002->001's quantity:2 and 002's quantity:1

My ShopCart class:
C#
DataView dv=new DataView();
    int rIndex=0;
    DataRow dr;

    public DataTable MakeCart()
    {
        DataTable tb = new DataTable("ShopCart");
        tb.Columns.Add("ProductID");
        tb.Columns.Add("quantity");
        tb.Columns.Add("price");
        tb.Columns.Add("total");
        return tb;
    }

    public DataTable AddCart(string ProductID,int price)
    {
        DataTable tbCart = new DataTable();
        tbCart = (DataTable)HttpContext.Current.Session["Cart"];
        if (tbCart == null)
        {
            tbCart = MakeCart();
        }
        dv = tbCart.DefaultView;
        dv.Sort = "ProductID";
        rIndex = dv.Find(ProductID);
        if (rIndex != -1)
        {
            tbCart.Rows[rIndex][1] = Convert.ToInt32(tbCart.Rows[rIndex][1]) + 1;
            tbCart.Rows[rIndex][3] = Convert.ToInt32(tbCart.Rows[rIndex][1]) * Convert.ToInt32(tbCart.Rows[rIndex][2]);
        }
        else
        {
            dr = tbCart.NewRow();
            dr[0] = ProductID;
            dr[1] = 1;
            dr[2] = price;
            dr[3] = price;
            //tbCart.AcceptChanges();
            tbCart.Rows.Add(dr);
        }
        return tbCart;
    }

event when click Add button:
C#
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataTable tb = new DataTable();
        string name = e.CommandName;
        if (name == "Add")
        {
                Label IDP = (Label)e.Item.FindControl("lblProductID");
                IDProduct = IDP.Text;
                tb = scart.AddCart(IDProduct, 20000);
                
        }
        Session["Cart"] = tb;
        Response.Redirect("Cart.aspx");
    }
Posted

1 solution

The way you have implemented, looks like AddCart method is getting fired for both item1 & item2.

Look here on how to implement DataList button click: Enable DataList Row Highliting and Click Event Postback[^]

Use Visual Studio DEBUGGER and see the workflow on what value is triggering the AddCart();
 
Share this answer
 
Comments
giatuan2011 11-Jun-12 6:52am    
Thank you.I think I should get value from GridView and re-update Datatable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900