public void AddToCart(string shoppingCartID, int productID, int quantity)
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                ShoppingCartItem myItem =
                    (from c in db.ShoppingCartItems
                     where c.ShoppingCartID == shoppingCartID && c.ProductID == productID
                     select c).FirstOrDefault();
                if (myItem == null)
                {
                    var cartadd = new ShoppingCartItem
                        {
                            ShoppingCartID = shoppingCartID,
                            Quantity = quantity,
                            ProductID = productID,
                            DateCreated = DateTime.Now,
                            ModifiedDate = DateTime.Now
                        };
                    db.ShoppingCartItems.AddObject(cartadd);
                }
                else
                {
                    myItem.Quantity += quantity;
                }

                db.SaveChanges();
            }
        }
Example #2
0
 public void CreateProductInDatabase(Product product)
 {
     using (var db = new AdventureWorks2008R2Entities())
     {
         db.Products.AddObject(product);
         db.SaveChanges();
     }
 }
        private int RemoveFromCart(string shoppingCartId, int cartItemId)
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                int itemCount = 0;
                //Get the cart
                var cartItem = db.ShoppingCartItems.Single(
                    cart => cart.ShoppingCartID == shoppingCartId
                            && cart.ShoppingCartItemID == cartItemId);

                if (cartItem != null)
                {
                    if (cartItem.Quantity > 1)
                    {
                        cartItem.Quantity--;
                        itemCount = cartItem.Quantity;
                    }
                    else
                    {
                        db.ShoppingCartItems.DeleteObject(cartItem);
                    }
                    db.SaveChanges();
                }

                return itemCount;
            }
        }