protected void submit_Click(object sender, EventArgs e) { db = new eWorldEntities(); using (usersShoppingCart = new ShoppingCartActions()) { List<Cart> c = usersShoppingCart.GetCartItems(); Order order = new Order(); order.OrderDate = c[0].DateCreated; order.Email = HttpContext.Current.User.Identity.Name; order.Total = Convert.ToDouble(usersShoppingCart.GetTotal()); order.paid = false; db.Orders.Add(order); db.SaveChanges(); OrderDetail orderdetail; int num = c.Count; for (int i = 0; i < num; i++) { orderdetail = new OrderDetail(); orderdetail.OrderID = order.OrderID; orderdetail.ProductID = c[i].ProductID; orderdetail.Quantity = c[i].Quantity; orderdetail.UnitPrice = c[i].Product.Price; db.OrderDetails.Add(orderdetail); db.SaveChanges(); } string ShoppingCartId = usersShoppingCart.GetCartId(); var cartItems = db.Carts.Where( ci => ci.CartID == ShoppingCartId); foreach (var cartItem in cartItems) { db.Carts.Remove(cartItem); } // Save changes. db.SaveChanges(); Response.Redirect("Checkout_success"); } }
public bool AddProduct(string ProductName, string ProductDesc, string ProductDetail, string ProductPrice, string ProductReview, int Stock, string Gift, string Sale, string vendor, Boolean onsale, string oldprice, string ProductImagePath1, string ProductImagePath2, string ProductImagePath3, string ProductImagePath4, Boolean show, List<string> cat_id) { var myProduct = new Product(); myProduct.ProductName = ProductName; myProduct.Description = ProductDesc; myProduct.Detail = ProductDetail; myProduct.Price = Double.Parse(ProductPrice); myProduct.Review = ProductReview; myProduct.StockID = Stock; myProduct.Gift = Gift; myProduct.Sale = Sale; myProduct.Vendor = vendor; myProduct.onSale = onsale; if (oldprice != "") myProduct.oldPrice = Double.Parse(oldprice); myProduct.ImagePath1 = ProductImagePath1; myProduct.ImagePath2 = ProductImagePath2; myProduct.ImagePath3 = ProductImagePath3; myProduct.ImagePath4 = ProductImagePath4; myProduct.Show = show; using (eWorldEntities db = new eWorldEntities()) { // Add product to DB. db.Products.Add(myProduct); foreach (string id in cat_id) { var cat = new ProductInCategory(); cat.CategoryID = Convert.ToInt32(id); myProduct.ProductID = myProduct.ProductID; db.ProductInCategories.Add(cat); } db.SaveChanges(); } // Success. return true; }