protected void btnPurchase_Click(object sender, EventArgs e) { int count = grdProduct.Rows.Count; int userId = Convert.ToInt32(lblUsername.Text); lblError.Text = ""; bool isResults = false; if (count > 0) { for (int i = 0; i < count; i++) { int prodId = GetProductId(grdProduct.Rows[i].Cells[0].Text, "Admin"); int remainingProd = GetProductNo(prodId); //string hired_table tbl = new hired_table(); TextBox quant = (TextBox)grdProduct.Rows[i].FindControl("txtQuantity"); if (Convert.ToInt32(quant.Text) < 1) { isResults = false; lblError.Text = grdProduct.Rows[i].Cells[0].Text + " Quantity cannot be less than 1"; break; } else if (remainingProd > Convert.ToInt32(quant.Text)) { isResults = true; } else if (remainingProd < Convert.ToInt32(quant.Text)) { isResults = false; lblError.Text = "Only " + remainingProd + " items remaining for " + grdProduct.Rows[i].Cells[0].Text; break; } } if (isResults) { System.Threading.Thread.Sleep(1000); pnlPayment.CssClass = "show_panel"; pnlProducts.CssClass = "hide_panel"; //GridViewRow rowProduct = (sender as Button).NamingContainer as GridViewRow; //DataTable dtProduct = ViewState["dtProducts"] as DataTable; //if (grdProduct.Rows.Count > 0) //{ // int index = rowProduct.RowIndex; // TextBox quant = (TextBox)grdProduct.Rows[index].FindControl("txtQuantity"); // TextBox nodays = (TextBox)grdProduct.Rows[index].FindControl("txtNoDays"); // string prodName = dtProduct.Rows[rowProduct.RowIndex]["name"].ToString(); // int quantity = Convert.ToInt32(quant.Text); // int NoDays = Convert.ToInt32(nodays.Text); // decimal price = Convert.ToDecimal(dtProduct.Rows[rowProduct.RowIndex]["price"]); // //quantity = quantity + 1; // dtProduct.Rows[rowProduct.RowIndex]["quantity"] = quantity; // dtProduct.Rows[rowProduct.RowIndex]["days"] = NoDays.ToString(); // dtProduct.Rows[rowProduct.RowIndex]["total_price"] = quantity * price * NoDays; // ViewState["dtProducts"] = dtProduct; // BindGrid(); //} } } }
public void SaveOrder() { CricketSystemEntities context = new CricketSystemEntities(); //calling delivery table int count = grdProduct.Rows.Count; var id = context.hired_table.Count() + 1; int userId = Convert.ToInt32(lblUsername.Text); if (count > 0) { for (int i = 0; i < count; i++) { int prodId = GetProductId(grdProduct.Rows[i].Cells[0].Text, "Admin"); //string hired_table tbl = new hired_table(); TextBox quant = (TextBox)grdProduct.Rows[i].FindControl("txtQuantity"); TextBox days = (TextBox)grdProduct.Rows[i].FindControl("txtNoDays"); tbl.Name = grdProduct.Rows[i].Cells[0].Text; tbl.Hire_No = id; tbl.User_id = Convert.ToInt32(lblUsername.Text); tbl.Username = GetUsername(userId); tbl.Price = Convert.ToDecimal(grdProduct.Rows[i].Cells[1].Text); tbl.Quantity = Convert.ToInt32(quant.Text); tbl.NoDays = Convert.ToInt32(days.Text); tbl.Date = DateTime.Now.ToShortDateString(); tbl.Time = DateTime.Now.ToShortTimeString(); tbl.Status = "Pending"; tbl.ReturnDate = DateTime.Now.AddDays(Convert.ToInt32(days.Text)).ToString(); UpdateStock(tbl.Quantity, tbl.Name); //sendEmail(); //delivery context.hired_table.Add(tbl); } try { context.SaveChanges(); SavePaymentDetails(id); System.Threading.Thread.Sleep(1000); Response.Redirect("ConfirmHire.aspx"); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); raise = new InvalidOperationException(message, raise); } } throw raise; } } }