这是我的“加入购物车”系统,当多次加入同一个商品时,(数量和总价计算都正常),但如果加入了另一个商品时,就出问题了。DataTable 中总会多出不需要的行。
请大家帮我看看。急用!protected void BtnAddToCar_Click(object sender, EventArgs e)
    {
        int goodsId = Int32.Parse(this.DetailsView1.Rows[0].Cells[1].Text);
        string theGoodsName = (this.DetailsView1.Rows[1].Cells[1].Text).ToString();
        decimal thePrice = Convert.ToDecimal(this.DetailsView1.Rows[5].Cells[1].Text);        //DataTable dtb = Session["myCar"] as DataTable;        if (Session["myCar"] == null)
        {
            DataTable dtb = new DataTable();
            dtb.Columns.Add("goodsId", typeof(int));
            dtb.Columns.Add("thtGoodsName");
            dtb.Columns.Add("thePrice");
            dtb.Columns.Add("TheCount");
            dtb.Columns.Add("TotalPrice");
            DataRow dRow = dtb.NewRow();
            dRow["goodsId"] = goodsId;
            dRow["thtGoodsName"] = theGoodsName;
            dRow["thePrice"] = thePrice;
            dRow["TheCount"] = 1;
            dRow["TotalPrice"] = thePrice;
            dtb.Rows.Add(dRow);
            Session["myCar"] = dtb;        }        else
        {
            DataTable dtb = Session["myCar"] as DataTable;
            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                if (Convert.ToInt32(dtb.Rows[i][0]) == goodsId)
                {
                    DataRow oldDR;
                    oldDR = dtb.Rows[i];
                    oldDR["TheCount"] = Int32.Parse(oldDR["TheCount"].ToString()) + 1;
                    oldDR["thePrice"] = thePrice;                    oldDR["TotalPrice"] = thePrice * Int32.Parse(oldDR["TheCount"].ToString());
                }
                else
                {
                    DataRow dRow = dtb.NewRow();
                    dRow["goodsId"] = goodsId;
                    dRow["thtGoodsName"] = theGoodsName;
                    dRow["thePrice"] = thePrice;
                    dRow["TheCount"] = 1;
                    dRow["TotalPrice"] = thePrice;
                    dtb.Rows.Add(dRow);
                }
                Session["myCar"] = dtb;
            }
        }
       Response.Redirect("myCar.aspx");
    }

解决方案 »

  1.   

    for(){}里边不应该有else{}逻辑。
      

  2.   

    逻辑明显有问题bool isInCart = false;
    for (int i = 0; i < dtb.Rows.Count; i++)
    {
    if (Convert.ToInt32(dtb.Rows[i][0]) == goodsId)
    {
      isInCart = true;

    }if(isInCart)
    {
    }
    else
    {
    }
      

  3.   

    DataTable dtb = Session["myCar"] as DataTable;
    bool isExist = false;
                for (int i = 0; i < dtb.Rows.Count; i++)
                {
                    if (Convert.ToInt32(dtb.Rows[i][0]) == goodsId)
                    {
    DataRow oldDR;
                        oldDR = dtb.Rows[i];
                        oldDR["TheCount"] = Int32.Parse(oldDR["TheCount"].ToString()) + 1;
                        oldDR["thePrice"] = thePrice;                    oldDR["TotalPrice"] = thePrice * Int32.Parse(oldDR["TheCount"].ToString());                    isExist = true;
                        break;
                     }
                  }
                if(!isExist )
                { 
                        DataRow dRow = dtb.NewRow();
                        dRow["goodsId"] = goodsId;
                        dRow["thtGoodsName"] = theGoodsName;
                        dRow["thePrice"] = thePrice;
                        dRow["TheCount"] = 1;
                        dRow["TotalPrice"] = thePrice;
                        dtb.Rows.Add(dRow);
                 }
                 Session["myCar"] = dtb;
      

  4.   

    非常感谢各位,特别是:ken_flash(AnotherBug)  和 ychangh(片片浊) 。现在问题解决了。使用的是 ychangh(片片浊) 的方法。
    顺便再问一下ken_flash(AnotherBug),你的解决逻辑很好。如果使用您的方法: FOR 之外的 i 将如何得到。
    如:for (int i = 0; i < dtb.Rows.Count; i++)
                {
                    if (Convert.ToInt32(dtb.Rows[i][0]) == goodsId)
                    {
                        isInCart = true;
                    }
                }
                if (isInCart = true)
                {
                    DataRow oldDR;                oldDR = dtb.Rows[i];  // 此处的  i 将如何得到。
                    oldDR["TheCount"] = Int32.Parse(oldDR["TheCount"].ToString()) + 1;
                    oldDR["thePrice"] = thePrice;                oldDR["TotalPrice"] = thePrice * Int32.Parse(oldDR["TheCount"].ToString());
                }
                else
                {
                    DataRow dRow = dtb.NewRow();
                    dRow["goodsId"] = goodsId;
                    dRow["thtGoodsName"] = theGoodsName;
                    dRow["thePrice"] = thePrice;
                    dRow["TheCount"] = 1;
                    dRow["TotalPrice"] = thePrice;
                    dtb.Rows.Add(dRow);
                }
      

  5.   

    接着上面的问题:现在我要更新购物车资料,可怎么也得不到 textbox 中的数量。不管能解决否,我都会尽快给大家结分!谢谢大家。
     protected void btnUpdateCart_Click(object sender, EventArgs e)
        {
            DataTable dtb = (DataTable)Session["myCar"];
            foreach (GridViewRow grw in this.GridView2.Rows)
            {            int i = grw.RowIndex;
                TextBox TheGoodsCount = (TextBox)grw.FindControl("txtCount");            int theCount = Int32.Parse(TheGoodsCount.Text);            DataRow UpdateDR;
                UpdateDR = dtb.Rows[i];
                Decimal thePrice = Convert.ToDecimal(dtb.Rows[i]["thePrice"]);
                UpdateDR["TheCount"] = theCount;
                UpdateDR["TotalPrice"] = thePrice * theCount;            Session["myCar"] = dtb;