2017-08-18 98 views
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... 

回答

2

問題出在別的塊

Response.Cookies["ShoppingCart"].Value = Request.Cookies["ShoppingCart"].Value + 
"|" + book_id.ToString() + "," + title.ToString() + 
"," + quantity.ToString() + "," + price.ToString(); 

在這裏,你與新內容一起到一個新的Cookie指定以前cookie的值。對於例如如果你的餅乾中有「1,鞋子1,25」,並再次將同樣的產品加入購物車,你會得到類似「1,鞋子1,25 | 1鞋子1,25」的東西。

這個問題可以被多個解決辦法

  1. 分裂您的cookie和檢查book_id值
  2. 使用對象存儲車項目,並將它們保存在會話中。

方法首先

String[] carts = Request.Cookies["ShoppingCart"].Value.Split("|") 
bool bookIdExists = false; 
foreach(string cartDetail in carts) 
{ 
    string bookId = cartDetail.Split(",")[0]; 
    if(bookId == newBookId) 
    { 
    bookIdExists = true; 
    } 
} 
if(!bookIDExists) 
{ 
    //Add your new item to Cookie 
} 

第二種方法

public Class Cart 
{ 
    public int BookId {get; set;} 
    public string Title {get; set;} 
    public int Qty {get; set;} 
    public decimal Price {get; set} 
} 

List<Cart> carts =Session["Cart"]==null? new List<Cart>(): Session["Cart"] as List<Cart>; 
var existingCart = carts.Where(x=> x.BookId==newBookId).FirstOrDefault(); 
if(existingCart==null) 
{ 
    Cart c= new Cart { 
    BookId = newBookId, 
    Title =newTitle, 
    Qty = newQty, 
    Price =newPrice 
}; 
carts.Add(c); 
Session["Cart"] = carts; 
} 

你可能會用更好的辦法來解決這個問題。

但使用cookie來存儲您的購物車詳細信息是一個可怕的想法。

+0

感謝您的回答,您的建議給我正確的方式來解決,但我不能解決問題呢。 –