protected void AddOrderToDB(CartItem ci, int order_code) { CpDataContext db = new CpDataContext(); CpOrder order = new CpOrder(); //get current user order.cust_id = db.CpCustomers.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.CpOrders.InsertOnSubmit(order); CpProduct product = db.CpProducts.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) { CpDataContext db = new CpDataContext(); var v = db.CpCustomers.Where(cust => cust.cust_mail.Equals(tbEmail.Text)); if (v.Count() == 0) { CpCustomer c = new CpCustomer(); //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.CpCustomers.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; } }