这是我的“加入购物车”系统,当多次加入同一个商品时,(数量和总价计算都正常),但如果加入了另一个商品时,就出问题了。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)