public void CartIndex(int pid, int quantity) { if (quantity == 0) { return; } ShoppingCartRepository cartrepo = new ShoppingCartRepository(Properties.Settings.Default.Constr); int idValue = 0; if (Request.Cookies["cartid"] != null) { //delete cookie //HttpCookie myCookie = new HttpCookie("cartid"); //myCookie.Expires = DateTime.Now.AddDays(-1d); //Response.Cookies.Add(myCookie); idValue = int.Parse(Request.Cookies["cartid"].Value); } else { ShoppingCart s = new ShoppingCart(); s.Date = DateTime.Now; cartrepo.AddNewShopCart(s); idValue = s.Id; HttpCookie cookie = new HttpCookie("cartid", idValue.ToString()); Response.Cookies.Add(cookie); } CartItem c = new CartItem { ProductId = pid, ShoppingCartId = idValue, Quantity = quantity }; cartrepo.AddCartItems(c); }