public void UpdateItem(string updateCartID, int updateProductID, int quantity) { using (var _db = new StoreCar.Models.ProductContext()) { try { var myItem = (from c in _db.ShoppingCartItems where c.CartId == updateCartID && c.Product.ProductID == updateProductID select c).FirstOrDefault(); if (myItem != null) { myItem.Quantity = quantity; _db.SaveChanges(); } } catch (Exception exp) { throw new Exception("Błąd: Problem z bazą - " + exp.Message.ToString(), exp); } } }
public void RemoveItem(string removeCartID, int removeProductID) { using (var _db = new StoreCar.Models.ProductContext()) { try { var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Product.ProductID == removeProductID select c).FirstOrDefault(); if (myItem != null) { // Usunięcie obiektu w tym przypadku elementu z koszyka. _db.ShoppingCartItems.Remove(myItem); _db.SaveChanges(); } } catch (Exception exp) { throw new Exception("Błąd: Nie można usunąć z koszyka - " + exp.Message.ToString(), exp); } } }
protected void RemoveProductButton_Click(object sender, EventArgs e) { using (var _db = new StoreCar.Models.ProductContext()) { int productId = Convert.ToInt16(DropDownRemoveProduct.SelectedValue); var myItem = (from c in _db.Products where c.ProductID == productId select c).FirstOrDefault(); if (myItem != null) { _db.Products.Remove(myItem); _db.SaveChanges(); // Przeładuj stronę. string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()); Response.Redirect(pageUrl + "?ProductAction=remove"); } else { LabelRemoveStatus.Text = "Nie można usunąć produktu.."; } } }