protected void AddOrderToDB(CartItem ci, int order_code) { UthDataContext db = new UthDataContext(); UthOrder order = new UthOrder(); //get current user order.cust_id = db.UthCustomers.Single(c => c.cust_mail.Equals(User.Identity.Name)).cust_id; order.ord_code = order_code; order.ord_date = DateTime.Now; order.ord_delivered = false; order.ord_quantity = ci.Quantity; order.prod_id = ci.ProductId; db.UthOrders.InsertOnSubmit(order); UthProduct product = db.UthProducts.Single(p => p.prod_id == ci.ProductId); //substract <quantity> units from stock for this product //we can't have negative values in stock! int tempstock = product.prod_stock - order.ord_quantity; if (product.prod_stock > 0 && tempstock > 0) { product.prod_stock = tempstock; } //write changes db.SubmitChanges(); }
protected void btnRegister_Click(object sender, EventArgs e) { if (this.IsValid) { UthDataContext db = new UthDataContext(); var v = db.UthCustomers.Where(cust => cust.cust_mail.Equals(tbEmail.Text)); if (v.Count() == 0) { UthCustomer c = new UthCustomer(); //c.cust_country =tbCountry.Text; c.cust_country_code = int.Parse(ddlCountry.SelectedItem.Value); c.cust_mail = tbEmail.Text; c.cust_name = tbName.Text; c.cust_password = tbPassword.Text; c.cust_phone = tbPhone.Text; c.cust_postalcode = int.Parse(tbPostal.Text); c.cust_streetname = tbStreet.Text; c.cust_streetnumber = int.Parse(tbHouse.Text); c.cust_city = tbCity.Text; db.UthCustomers.InsertOnSubmit(c); db.SubmitChanges(); bool authenticated = Customer.AuthenticateCustomer(c.cust_mail, c.cust_password); if (authenticated) { FormsAuthentication.RedirectFromLoginPage(c.cust_mail, false); } Response.Redirect("~/ThankYou.aspx"); } pError.Visible = true; } }