购物车中的数量更新问题 [英] problem with updating the quantity in shopping cart

查看:69
本文介绍了购物车中的数量更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作购物车.我使用此sql查询仅显示了产品ID产品名称单价数量

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+"''"; 



现在我在购物车中有一个更新按钮..

当我更新数量时..它简单地更新购物车表中的数量并在标签中显示总金额,我将其放置在gridview中数量的页脚模板中.这是代码.但是购物车甚至都没有更新..
错误索引超出范围异常

任何人都可以找出问题所在吗?
这是代码



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)
    {
        try
        {
            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");
                    }
                    ShoppingCart.UpdateItem(item);
                }
            }
           // GridView1.DataBind();
            bind();
            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");
        } 

    }
}

推荐答案

您是否设置了
Did you set the DataKeyNames[^] property? Have you checked?


这篇关于购物车中的数量更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆