protected void RemoveProductButton_Click(object sender, EventArgs e)
        {
            //u ovoj metodi uzimamo selectovni proizvod iz liste na osnovu DataValueField = "ProductID"
            //onda pokusamo da "dovucemo" taj proizvod iz tabele Products.
            //imamo proveru da li je null jer FirstOrDefault vraca null ukoliko ne pronadje taj proizvod.
            //ako nije NULL brisemo ga iz tabele
            //i potrebno je pozvati metodu na kontextu
            //kako bi se izmene sacuvale.
            using (var _db = new ProdavnicaIgracaka.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();

                    // Reload the page.
                    string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
                    Response.Redirect(pageUrl + "?ProductAction=remove");
                }
                else
                {
                    LabelRemoveStatus.Text = "Nije moguce pronaci proizvod.";
                }
            }
        }
예제 #2
0
 //Struktura ShoppingCartUpdates koristi se za držanje svih stavki korpe za kupovinu.
 //Metod UpdateShoppingCartDatabase koristi strukturu ShoppingCartUpdates da bi odredio da li je potrebno ažurirati
 //ili ukloniti neku od stavki.
 public void UpdateItem(string updateCartID, int updateProductID, int quantity)
 {
     using (var _db = new ProdavnicaIgracaka.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("ERROR: Unable to Update Cart Item - " + exp.Message.ToString(), exp);
         }
     }
 }
예제 #3
0
 public void RemoveItem(string removeCartID, int removeProductID)
 {
     using (var _db = new ProdavnicaIgracaka.Models.ProductContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Product.ProductID == removeProductID select c).FirstOrDefault();
             if (myItem != null)
             {
                 _db.ShoppingCartItems.Remove(myItem);
                 _db.SaveChanges();
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Remove Cart Item - " + exp.Message.ToString(), exp);
         }
     }
 }