//------------------------------------------------------------------------------------------------------------------------------------------+ protected void Page_Load(object sender, EventArgs e) { if (!String.IsNullOrEmpty(Request.QueryString["ProductID"])) { int productID = 0; Int32.TryParse(Request["productID"].ToString(), out productID); using (CommerceEntities db = new CommerceEntities()) { try { var thisProduct = (from p in db.Products where p.ProductID == productID select p).FirstOrDefault(); ModelName.Text = thisProduct.ModelName; } catch (Exception exp) { throw new Exception("ERROR: Unable to Add Product Review - " + exp.Message.ToString(), exp); } } } else { Debug.Fail("ERROR : We should never get to ReviewAdd.aspx without a ProductId."); throw new Exception("ERROR : It is illegal to load ReviewAdd.aspx without setting a ProductId."); } }
//------------------------------------------------------------------------------------------------------------------------------------------+ protected void Page_PreRender(object sender, EventArgs e) { if (_ProductId < 1) { // This should never happen but we could expand the use of this control by reducing the // dependency on the query string by selecting a few RANDOME products here. Debug.Fail("ERROR : The Also Purchased Control Can not be used without setting the ProductId."); throw new Exception("ERROR : It is illegal to load the AlsoPurchased COntrol without setting a ProductId."); } int ProductCount = 0; using (CommerceEntities db = new CommerceEntities()) { try { var v = db.SelectPurchasedWithProducts(_ProductId); ProductCount = v.Count(); } catch (Exception exp) { throw new Exception("ERROR: Unable to Retrieve Also Purchased Items - " + exp.Message.ToString(), exp); } } if (ProductCount > 0) { WriteAlsoPurchased(_ProductId); } else { WritePopularItems(); } }
protected void Page_Load(object sender, EventArgs e) { using (CommerceEntities db = new CommerceEntities()) { try { var query = (from ProductOrders in db.OrderDetails join SelectedProducts in db.Products on ProductOrders.ProductID equals SelectedProducts.ProductID group ProductOrders by new { ProductId = SelectedProducts.ProductID, ModelName = SelectedProducts.ModelName } into grp select new { ModelName = grp.Key.ModelName, ProductId = grp.Key.ProductId, Quantity = grp.Sum(o => o.Quantity) } into orderdgrp where orderdgrp.Quantity > 0 orderby orderdgrp.Quantity descending select orderdgrp).Take(5); RepeaterItemsList.DataSource = query; RepeaterItemsList.DataBind(); } catch (Exception exp) { throw new Exception("ERROR: Unable to Load Popular Items - " + exp.Message.ToString(), exp); } } }
//------------------------------------------------------------------------------------------------------------------------------------------+ public void AddItem(string cartID, int productID, int quantity) { using (CommerceEntities db = new CommerceEntities()) { try { var myItem = (from c in db.ShoppingCarts where c.CartID == cartID && c.ProductID == productID select c).FirstOrDefault(); if (myItem == null) { ShoppingCart cartadd = new ShoppingCart(); cartadd.CartID = cartID; cartadd.Quantity = quantity; cartadd.ProductID = productID; cartadd.DateCreated = DateTime.Now; db.ShoppingCarts.AddObject(cartadd); } else { myItem.Quantity += quantity; } db.SaveChanges(); } catch (Exception exp) { throw new Exception("ERROR: Unable to Add Item to Cart - " + exp.Message.ToString(), exp); } } }
//------------------------------------------------------------------------------------------------------------------------------------------+ private void WriteAlsoPurchased(int currentProduct) { using (CommerceEntities db = new CommerceEntities()) { try { var v = db.SelectPurchasedWithProducts(currentProduct); RepeaterItemsList.DataSource = v; RepeaterItemsList.DataBind(); } catch (Exception exp) { throw new Exception("ERROR: Unable to Write Also Purchased - " + exp.Message.ToString(), exp); } } }
//------------------------------------------------------------------------------------------------------------------------------------------+ public decimal GetTotal(string cartID) { using (CommerceEntities db = new CommerceEntities()) { decimal cartTotal = 0; try { var myCart = (from c in db.ViewCarts where c.CartID == cartID select c); if (myCart.Count() > 0) { cartTotal = myCart.Sum(od => (decimal)od.Quantity * (decimal)od.UnitCost); } } catch(Exception exp) { throw new Exception("ERROR: Unable to Calculate Order Total - " + exp.Message.ToString(), exp); } return (cartTotal); } }
//------------------------------------------------------------------------------------------------------------------------------------------+ protected void ReviewAddBtn_Click(object sender, ImageClickEventArgs e) { if (Page.IsValid == true) { // Obtain ProductID from Page State int productID = Int32.Parse(Request["productID"]); // Obtain Rating number of RadioButtonList int rating = Int32.Parse(Rating.SelectedItem.Value); // Add Review to ReviewsDB. HtmlEncode before entry using (CommerceEntities db = new CommerceEntities()) { try { ShoppingCart cartadd = new ShoppingCart(); Review newReview = new Review() { ProductID = productID, Rating = rating, CustomerName = HttpUtility.HtmlEncode(Name.Text), CustomerEmail = HttpUtility.HtmlEncode(Email.Text), Comments = HttpUtility.HtmlEncode(UserComment.Content) }; db.Reviews.AddObject(newReview); db.SaveChanges(); } catch (Exception exp) { throw new Exception("ERROR: Unable to Update Cart Item - " + exp.Message.ToString(), exp); } } Response.Redirect("ProductDetails.aspx?ProductID=" + productID); } Response.Redirect("ProductList.aspx"); }
//------------------------------------------------------------------------------------------------------------------------------------------+ public void UpdateShoppingCartDatabase(String cartId, ShoppingCartUpdates[] CartItemUpdates) { using (CommerceEntities db = new CommerceEntities()) { try { int CartItemCOunt = CartItemUpdates.Count(); var myCart = (from c in db.ViewCarts where c.CartID == cartId select c); foreach (var cartItem in myCart) { // Iterate through all rows within shopping cart list for (int i = 0; i < CartItemCOunt; i++) { if (cartItem.ProductID == CartItemUpdates[i].ProductId) { if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true) { RemoveItem(cartId, cartItem.ProductID); } else { UpdateItem(cartId, cartItem.ProductID, CartItemUpdates[i].PurchaseQuantity); } } } } } catch (Exception exp) { throw new Exception("ERROR: Unable to Update Cart Database - " + exp.Message.ToString(), exp); } } }
//------------------------------------------------------------------------------------------------------------------------------------------+ public void UpdateItem(string cartID, int productID, int quantity) { using (CommerceEntities db = new CommerceEntities()) { try { var myItem = (from c in db.ShoppingCarts where c.CartID == cartID && c.ProductID == productID 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); } } }
//------------------------------------------------------------------------------------------------------------------------------------------+ public bool SubmitOrder(string UserName) { using (CommerceEntities db = new CommerceEntities()) { try { //------------------------------------------------------------------------+ // Add New Order Record | //------------------------------------------------------------------------+ Order newOrder = new Order(); newOrder.CustomerName = UserName; newOrder.OrderDate = DateTime.Now; newOrder.ShipDate = CalculateShipDate(); db.Orders.AddObject(newOrder); db.SaveChanges(); //------------------------------------------------------------------------+ // Create a new OderDetail Record for each item in the Shopping Cart | //------------------------------------------------------------------------+ String cartId = GetShoppingCartId(); var myCart = (from c in db.ViewCarts where c.CartID == cartId select c); foreach (ViewCart item in myCart) { int i = 0; if (i < 1) { OrderDetail od = new OrderDetail(); od.OrderID = newOrder.OrderID; od.ProductID = item.ProductID; od.Quantity = item.Quantity; od.UnitCost = item.UnitCost; db.OrderDetails.AddObject(od); i++; } var myItem = (from c in db.ShoppingCarts where c.CartID == item.CartID && c.ProductID == item.ProductID select c).FirstOrDefault(); if (myItem != null) { db.DeleteObject(myItem); } } db.SaveChanges(); } catch (Exception exp) { throw new Exception("ERROR: Unable to Submit Order - " + exp.Message.ToString(), exp); } } return(true); }
//------------------------------------------------------------------------------------------------------------------------------------------+ public void MigrateCart(String oldCartId, String UserName) { using (CommerceEntities db = new CommerceEntities()) { try { var myShoppingCart = from cart in db.ShoppingCarts where cart.CartID == oldCartId select cart; foreach (ShoppingCart item in myShoppingCart) { item.CartID = UserName; } db.SaveChanges(); Session[CartId] = UserName; } catch (Exception exp) { throw new Exception("ERROR: Unable to Migrate Shopping Cart - " + exp.Message.ToString(), exp); } } }