0
問題是,當再次將相同的項目添加到購物車時,它顯示同一產品的同一記錄兩次,這裏我想要的是,當我按兩次或三次按下添加到購物車按鈕然後它應該是同一項目的增量。購物車顯示記錄兩次
protected void AddToCartButton_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
book_id = Convert.ToInt32(Request.QueryString["book_id"].ToString());
cmd.CommandText = "select * from Book where book_id = " + book_id + "";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
//book_id =Convert.ToInt32(dr["book_id"]);
title = dr["title"].ToString();
//quantity = dr["quantity"].ToString();
quantity = QuantityTxt.Text;
price = dr["price"].ToString();
}
//Restrict User to enter some quantity that he wants...
if(Convert.ToInt32(QuantityTxt.Text)>Convert.ToInt32(quantity))
{
ErrorMsglbl.Text = "Please Enter Lower Quantity";
}
else
{
ErrorMsglbl.Text = "";
}
// Creating Cookies to store the value and then i will assign to data table...
if (Request.Cookies["ShoppingCart"] == null)
{
Response.Cookies["ShoppingCart"].Value = book_id.ToString() + "," + title.ToString() + "," + quantity.ToString() + "," + price.ToString();
Response.Cookies["ShoppingCart"].Expires = DateTime.Now.AddDays(1);
}
else
{
Response.Cookies["ShoppingCart"].Value = Request.Cookies["ShoppingCart"].Value + "|" + book_id.ToString() + "," + title.ToString() + "," + quantity.ToString() + "," + price.ToString();
Response.Cookies["ShoppingCart"].Expires = DateTime.Now.AddDays(1);
}
Response.Redirect("BookDetails.aspx?book_id="+book_id.ToString()+"&Cat_ID="+ViewState["cat"]+"");
}// End Add To Cart Button...
感謝您的回答,您的建議給我正確的方式來解決,但我不能解決問題呢。 –