protected void registerBtn_Click(object sender, EventArgs e) { if (Page.IsValid) { using (ECTDBContext entities = new ECTDBContext()) { var member = entities.Customers.Create(); var address = entities.Addresses.Create(); member.FirstName = firstNameTxt.Text; member.LastName = lastNameTxt.Text; member.Email = emailTxt.Text; member.UserName = userNameTxt2.Text; member.Password = SecuredPassword.GenerateHash(passwordTxt2.Text); member.DateRegistered = DateTime.Now.Date; entities.Customers.Add(member); entities.SaveChanges(); var pwdHash = SecuredPassword.GenerateHash(passwordTxt2.Text); var user = (from u in entities.Customers where u.UserName == userNameTxt2.Text && u.Password == pwdHash select u).FirstOrDefault(); address.CustomerID = user.ID; address.Address1 = addressTxt.Text; address.City = cityTxt.Text; address.State = stateTxt.Text; address.AddressType = "Billing"; entities.Addresses.Add(address); entities.Addresses.Add(address); entities.SaveChanges(); clearTextBoxes(registerControl); registerSuccessLbl.Visible = true; } } }
protected void update_Click(object sender, EventArgs e) { if (Page.IsValid) { pnlMain.Visible = true; pnlUpdate.Visible = false; using (ECTDBContext entities = new ECTDBContext()) { var userID = Int32.Parse(Session["LoggedInId"].ToString()); var userInfo = (from u in entities.Customers where u.ID == userID select u).FirstOrDefault(); var userAddress = (from a in entities.Addresses where a.CustomerID == userID select a).FirstOrDefault(); userInfo.Email = emailTxt.Text; userInfo.FirstName = firstNameTxt.Text; userInfo.LastName = lastNameTxt.Text; userInfo.UserName = userNameTxt2.Text; userAddress.Address1 = addressTxt.Text; userAddress.State = stateTxt.Text; userAddress.City = cityTxt.Text; entities.SaveChanges(); } Response.Redirect("Account.aspx"); } }
public void removeFromCart(Object sender, EventArgs e) { if (Session["update"].ToString() == ViewState["update"].ToString()) { Button b = (Button)sender; //store the commandargument attached to the delete button int id = Convert.ToInt32(b.CommandArgument.ToString()); using (ECTDBContext context = new ECTDBContext()) { var orderDetail = (from o in context.OrderDetails where o.ID == id select o).FirstOrDefault(); Decimal deletePrice = orderDetail.Cost * orderDetail.Quantity; context.OrderDetails.Remove(orderDetail); context.SaveChanges(); //update cart item number at top of page int cartCount = int.Parse(Session["cartCount"].ToString()); cartCount -= orderDetail.Quantity; Session["cartCount"] = cartCount; Master.CartText = cartCount.ToString(); var updatedOrder = (from order in context.Orders where order.CustomerID == userID && order.IsCart == "true" select order).ToList(); var newOrder = updatedOrder.First(); newOrder.Total -= deletePrice; context.SaveChanges(); var updatedOrderDetail = (from od in context.OrderDetails where od.OrderID == newOrder.ID select od).ToList(); summaryListView.DataSource = updatedOrderDetail; totalListView.DataSource = updatedOrder; summaryListView.DataBind(); totalListView.DataBind(); } Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); } else { return; } }
public void createNewCart(int itemID, int quantity) { using (ECTDBContext context = new ECTDBContext()) { //Edit the cart number at top of page int cartCount = int.Parse(Session["cartCount"].ToString()); cartCount += quantity; Session["cartCount"] = cartCount; Master.CartText = cartCount.ToString(); //Build new Order entry var newOrder = new Order(); newOrder.CustomerID = userID; newOrder.IsCart = "true"; newOrder.Total = 0; newOrder.ShippingID = 1; newOrder.ShippingAddressID = 1; context.Orders.Add(newOrder); context.SaveChanges(); //Build new OrderDetail entry var newOrderDetail = new OrderDetail(); //find new order var newOrderVar = (from order in context.Orders where order.IsCart == "true" && order.CustomerID == userID select order).FirstOrDefault(); newOrderDetail.OrderID = newOrderVar.ID; var product = (from p in context.Products where p.ID == itemID select p).FirstOrDefault(); newOrderDetail.Cost = product.Cost; newOrderDetail.Quantity = quantity; newOrderDetail.ProductID = itemID; newOrderDetail.ProductName = product.Name; newOrderVar.Total += newOrderDetail.Cost * newOrderDetail.Quantity; context.OrderDetails.Add(newOrderDetail); context.SaveChanges(); } }
public void placeOrderClick(object sender, EventArgs e) { if (Session["update"].ToString() == ViewState["update"].ToString()) { using (ECTDBContext context = new ECTDBContext()) { var order = (from or in context.Orders where or.CustomerID == userID && or.IsCart == "true" select or).FirstOrDefault(); if (order != null) { String cost = order.Total.ToString(); String orderID = order.ID.ToString(); var orderDetailList = (from od in context.OrderDetails where od.OrderID == order.ID select od).ToList(); foreach (OrderDetail od in orderDetailList) { if (od.OrderID == order.ID) { int quantity = od.Quantity; int productID = od.ProductID; var product = (from p in context.Products where p.ID == productID select p).FirstOrDefault(); product.TotalOnHand -= quantity; order.IsCart = "false"; order.Date = DateTime.Now.ToString("MM/dd/yy"); Session["cartCount"] = "0"; context.SaveChanges(); RedirectUser(orderID, cost); } } Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); } } } else { return; } }
protected void passWordConfirm_Click(object sender, EventArgs e) { if (Page.IsValid) { pnlMain.Visible = true; orderHistory.Visible = true; pnlPassword.Visible = false; using (ECTDBContext entities = new ECTDBContext()) { var pwdHash = SecuredPassword.GenerateHash(passwordTxt.Text); var userID = Int32.Parse(Session["LoggedInId"].ToString()); var customer = (from c in entities.Customers where c.ID == userID select c).FirstOrDefault(); if (customer.Password == pwdHash && newPasswordTxt.Text.ToString().Length > 0) { customer.Password = SecuredPassword.GenerateHash(newPasswordTxt.Text); entities.SaveChanges(); } } } }
public void addToCartClick(object sender, EventArgs e) { if (Session["update"].ToString() == ViewState["update"].ToString()) { using (ECTDBContext context = new ECTDBContext()) { Button b = (Button)sender; //store the commandargument attached to the delete button int id = Convert.ToInt32(b.CommandArgument.ToString()); //Get quantity value from textbox ListViewDataItem item = b.NamingContainer as ListViewDataItem; TextBox quantityText = item.FindControl("quantityText") as TextBox; int quantity = Convert.ToInt32(quantityText.Text); if (quantity == 0) { return; } else { var product = (from p in context.Products where p.ID == id select p).FirstOrDefault(); var userOrdersList = (from o in context.Orders where o.CustomerID == userID && o.IsCart == "true" select o).ToList(); //If user has an existing cart if (userOrdersList.Count > 0) { //Edit the cart number at top of page int cartCount = int.Parse(Session["cartCount"].ToString()); cartCount += quantity; Session["cartCount"] = cartCount; this.Master.CartText = cartCount.ToString(); var userOrders = userOrdersList.First(); var repeatOrderDetail = (from or in context.OrderDetails where or.OrderID == userOrders.ID && or.ProductID == id select or).FirstOrDefault(); if (repeatOrderDetail != null) { repeatOrderDetail.Quantity += quantity; userOrders.Total += quantity * product.Cost; context.SaveChanges(); var orderDetailSummary = (from orderDetail in context.OrderDetails where orderDetail.OrderID == userOrders.ID select orderDetail).ToList(); var updatedUserOrderList = (from o in context.Orders where o.CustomerID == userID && o.IsCart == "true" select o).ToList(); } else { //Build new OrderDetail entry var newOrderDetail = new OrderDetail(); newOrderDetail.ProductID = id; //set orderID to the current order newOrderDetail.OrderID = userOrders.ID; newOrderDetail.Cost = product.Cost; newOrderDetail.ProductName = product.Name; newOrderDetail.Quantity = quantity; //update the running total for entire order userOrders.Total += newOrderDetail.Cost * newOrderDetail.Quantity; context.OrderDetails.Add(newOrderDetail); context.SaveChanges(); } } //if user does not have an existing cart else { createNewCart(id, quantity); } } } Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); } else { return; } }