c# - Shopping cart displays records twice -


problem when same item added cart again display same record of same product twice, here want when press add cart button twice or 3 time should increment quantity of same item.

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 * book 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 enter quantity wants...     if(convert.toint32(quantitytxt.text)>convert.toint32(quantity))     {         errormsglbl.text = "please enter lower quantity";     }     else     {         errormsglbl.text = "";     }     // creating cookies store value , assign 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 cart button...  

problem lies in else block

 response.cookies["shoppingcart"].value = request.cookies["shoppingcart"].value +  "|" + book_id.tostring() + "," + title.tostring() +  "," + quantity.tostring() + "," + price.tostring(); 

here assigning previous cookie value new cookie along new content. e.g. if have "1, shoe, 1,25" in cookie , add same product cart again "1, shoe, 1,25| 1,shoe, 1,25".

this problem can solved multiple approaches

  1. split cookies , check book_id value
  2. use object store cart item , save them in session.

approach first

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 new item cookie } 

second approach

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"] 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; } 

you may come better approach solve problem.

but terrible idea use cookie store cart details.


Comments

Popular posts from this blog

The SDK directory does not exist (ubuntu 16.04) (react native) -

python - How to clone or copy a list? -

Convert excel to pdf with VBScript -