Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem. Presently I'm preparing a shopping cart website using asp.net and C#.net. Whenever client logs in I read the data form db and assign data table and assign to cart and load in grid view. My problem is at the time of edition the cart is not updated. It shows the exception. i.e deleted table can't assign the data into variables. But it will work for normal cart, i.e when purchase a product it will add to cart and db.
Can anybody help me?
My code is:
C#
objDT = (DataTable)HttpContext.Current.Session["Cart"];
            decimal subTotalBefDiscount = default(decimal);
            for (int i = 0; i < objDT.Rows.Count; i++)
            {
                objDR = objDT.Rows[i];
                string sQuantity = ((TextBox)gvCart.Rows[i].FindControl("txtQuantity")).Text;
                objDR["Quantity"] = sQuantity;
                objDR["Price"] = objDR["Price"];
                int qty = Convert.ToInt32(objDR["Quantity"].ToString());
                decimal price = Convert.ToDecimal(objDR["Price"]);
                objDR["Total"] = qty * price;
            }
            int rowCount = objDT.Rows.Count;
            //objDT.Reset();
            for (int i = rowCount - 1; i >= 0; i--)
            {
                CheckBox cbDelete = (CheckBox)gvCart.Rows[i].Cells[0].FindControl("chkDelete");
                if (cbDelete.Checked)
                {
                    objDT.Rows[i].Delete();
                }
            }
            for (int i = 0; i < objDT.Rows.Count; i++)
            {
                objDR = objDT.Rows[i];
                int qty = Convert.ToInt32(objDR["Quantity"].ToString());
                decimal price = Convert.ToDecimal(objDR["Price"]);
                decimal tot = qty * price;
                subTotalBefDiscount = subTotalBefDiscount + tot;
            }
            gvCart.DataSource = objDT;
            gvCart.DataBind();
            lblsubTotalBefDiscount.Text = subTotalBefDiscount.ToString();
            decimal QuantityDiscounts = Caluculate_QunatityDsicounts();
            lblQuantityDiscounts.Text = QuantityDiscounts.ToString();
            lblsubTotalAftDiscount.Text = (subTotalBefDiscount - QuantityDiscounts).ToString();
            if (gvCart.Rows.Count > 0)
            {
                plcShoppingCart.Visible = true;
                plcEmptyCart.Visible = false;
            }
            else
            {
                plcShoppingCart.Visible = false;
                plcEmptyCart.Visible = true;
            }
            StoreBillingInfoInSession();
        }
        catch (Exception Ex)
        {
            Response.Redirect("Error.aspx?M=" + Ex.Message);
        }

Thank you.
Posted
Updated 3-Jan-11 0:03am
v2
Comments
Manfred Rudolf Bihy 3-Jan-11 8:39am    
Hi Ajitha, please don't post additions to your question as an answer. Please add it to your question and I would really appreciate it if you could post the complete exception message and also the stack trace of that exception. Thank you for your cooperation!
Manfred Rudolf Bihy 3-Jan-11 16:09pm    
I found it at last! Please see my answer for the nescessary modifications to your code. Enjoy!

1 solution

I spotted that you are hiding the gridview when there are no rows to display so you don't need to do all the for loops and the binding in case of an empty cart. So if objDT.Rows.Count is zero you can go immeadiately to the part where you are hiding the gridview and
do the other stuff only when there is something I in your cart.

Modification:
OMG, I found the solution to your problem on MSDN. After the Delete call on the row you also have to call AcceptChanges(). Only then will the row really be removed from the table.

C#
// Going through the grid rows to see which entries shall be deletee
for (int i = rowCount - 1; i >= 0; i--)
{
    CheckBox cbDelete = (CheckBox)gvCart.Rows[i].Cells[0].FindControl("chkDelete");
    if (cbDelete.Checked)
    {
        objDT.Rows[i].Delete(); // So far so good the row can be removed from the table
        objDT.Rows[i].AcceptChanges(); // Now the row will now really be removed
    }
}

The above code should solve your issue.

End modification


Regards,
Manfred
 
Share this answer
 
v3
Comments
thatraja 3-Jan-11 12:17pm    
[Comment from OP, moved from answer]
Initially,when ever client click add to cart button. i added the product to data table. that table assigned to cart. that cart is showed in a data grid. and update and deletions are working good by using above code.
problem is, at the time of login i retrieve the cart data form db. and assign to cart.it is also working. and update and deletion for db data is not working. the same code and same cart is using for both. but at the time of deleting db cart it just delete data not row. thats why "Deleted row information cannot be accessed through the row" this error i get. any solution for this problem.
Dalek Dave 3-Jan-11 16:36pm    
Good Answer.
ajitha.pusapati 4-Jan-11 1:49am    
thank u very much., same solution i found in msdn. its really helpful.

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