Click here to Skip to main content
15,742,619 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to make a shopping cart. i displayed simply product id product name unit price quantity using this sql query

string query = "select Products.ProductID,Products.ProductName,Products.UnitCost,ShoppingCart.Quantity from Products RIGHT JOIN ShoppingCart on Products.ProductID=ShoppingCart.ProductID where CustomerID='"+User.Identity.Name+"'"; 

now i have a update button in a cart..

when i update the quantity .. it simple update the quantity in shopping cart table and show the total amount in a label which i put at the footer template of a quntity in gridview.. here is the code. but the cart is not even updating..
there is the error index is out of range exception

can anyone figure out the problem??
here is the code

protected void btnupdatecart_Click(object sender, EventArgs e)
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                GridViewRow row = GridView1.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                    ShoppingCartItem item = new ShoppingCartItem();
                    item.CustomerID = User.Identity.Name;
                    //item.ProductID = (int)GridView1.DataKeys[i].Value;
                    item.ProductID = int.Parse(((Label)(row.FindControl("lblproid"))).Text);
                    item.Quantity = int.Parse(((TextBox)(row.FindControl("txtdmainqty"))).Text);
                    if (item.Quantity <= 0)
                        throw new Exception("Invalid Quantity");
           // GridView1.DataBind();
            btnplaceorder.Enabled = true;
            //lblMsg.Text = "";
        catch (Exception ex)
            btnplaceorder.Enabled = false;
            //lblMsg.Text = ex.Message;

    private decimal decTotal;
    protected void _rowdatabound(object sender, GridViewRowEventArgs e)
         if (e.Row.RowType == DataControlRowType.DataRow)
            //int prodid = int.Parse(e.Row.Cells[0].Text);
             int prodid=int.Parse(((Label)(e.Row.FindControl("lblproid"))).Text);
            int qty = int.Parse(((TextBox)(e.Row.FindControl("txtmainqty"))).Text);
            Product p = Product.GetProduct(prodid);
            decTotal = decTotal + (qty * p.UnitCost);
        if (e.Row.RowType == DataControlRowType.Footer)
            ((Label)(e.Row.FindControl("lbltotal"))).Text = "Total :" + decTotal.ToString("C");

Updated 24-Sep-11 10:49am
Herman<T>.Instance 24-Sep-11 16:55pm    
if you debug:
on which line do you get the exception? Maybe show stacktrace!
And what are the results of
item.ProductID = int.Parse(((Label)(row.FindControl("lblproid"))).Text);
item.Quantity = int.Parse(((TextBox)(row.FindControl("txtdmainqty"))).Text);

It is pretty dangerous not to check on != null when doing a FindControl
codegeekalpha 24-Sep-11 17:09pm    
i am using template field in productid.. can i use this with a template field???

item.ProductID = (int)GridView1.DataKeys[i].Value.
what is the alternate of that if i am using templatefield.
it think we use it in databound field.
codegeekalpha 24-Sep-11 17:09pm    
i am getting this exception
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Sergey Alexandrovich Kryukov 24-Sep-11 22:45pm    
Use the debugger, that's it.

1 solution

Did you set the DataKeyNames[^] property? Have you checked?
Share this answer

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