I process the elements that users select from the gridview and perform the insertion of email and database.
When the user selects the button, the code below takes information from the gridview, creates a new order in the Order table and creates new records in the Transactions table.
How can I get the last inserted identifier if I use this method of inserting a record? Would you recommend a different approach to this simple problem?
protected void btnOrder_Click(object sender, EventArgs e)
{
double Total = 0;
string MailFrom = "webadmin@domain.com";
string MailTo = "purchasing@domain.com";
string MailSubject = "Online Order";
string MailCC = "";
string MailBCC = "";
string MailReplyTo = "";
string MailBody = "";
TextBox ItmCostCode = (TextBox)form1.FindControl("txtCostCode");
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl("ItemSelect");
Label ItmTotal = (Label)gvr.FindControl("ItmTotal");
Label ItmPrice = (Label)gvr.FindControl("ItmPrice");
Label ItmName = (Label)gvr.FindControl("lblName");
TextBox ItmQty = (TextBox)gvr.FindControl("ItmQty");
TextBox ItmID = (TextBox)gvr.FindControl("lblItemID");
SqlDataSource2.InsertParameters.Add("OrderDate", DateTime.Now.ToString("MMMM dd, yyyy"));
SqlDataSource2.InsertParameters.Add("OrderTotal", "0");
SqlDataSource2.InsertParameters.Add("OrderAccount", "name");
SqlDataSource2.InsertParameters.Add("OrderCostCentre", ItmCostCode.Text);
SqlDataSource2.Insert();
if (cb.Checked)
{
double Price = Convert.ToDouble(ItmPrice.Text);
double Qty = Convert.ToDouble(ItmQty.Text);
Total = Price * Qty;
OrderTotal = OrderTotal + Total;
MailBody = MailBody + "Item: "+ItmName.Text+" Quantity: "+ItmQty.Text+" Total: "+ItmTotal.Text+"\n\r";
SqlDataSource3.InsertParameters.Add("ItemID", ItmID.Text);
SqlDataSource3.InsertParameters.Add("OrderID", );
SqlDataSource3.InsertParameters.Add("Price", ItmPrice.Text);
SqlDataSource3.Insert();
}
}
string strOrderTotal = OrderTotal.ToString();
MailBody = MailBody+"Order Total: " + strOrderTotal+"\n\r";
MailBody = MailBody + "Cost Code: " + ItmCostCode.Text;
MailService.Service1 Mailer = new MailService.Service1();
Mailer.SendMail("Text", MailFrom, MailTo, MailCC, MailBCC, MailSubject, MailBody, MailReplyTo);
}
source
share