private void btnSave_Click(object sender, EventArgs e) { //Get the Values from PurchaseSales Form First transactionsBLL transaction = new transactionsBLL(); DeaCustBLL dc = new DeaCustBLL(); transaction.type = lblTop.Text; //Get the ID of or Dealer Customer Here //Lets get name of the dealer or customer first string deaCustName = txtName.Text; if (!string.IsNullOrEmpty(deaCustName)) { dc = dcDAL.GetDeaCustIDFromName(deaCustName); } else { DeaCustDAL dcd = new DeaCustDAL(); dc.type = "Walkin"; dc.name = "Walkin Customer"; dc.email = " "; dc.added_by = 4; dc.added_date = DateTime.Now; dcd.Insert(dc); // if customer / dealer name not exists its means Walkin Customer } transaction.dea_cust_id = dc.id; transaction.grandTotal = Math.Round(decimal.Parse(txtGrandTotal.Text), 2); transaction.transaction_date = DateTime.Now; transaction.tax = decimal.Parse(txtVat.Text); transaction.discount = decimal.Parse(txtDiscount.Text); //Get the Username of Logged in user string username = frmLogin.loggedIn; int branchId = int.Parse(frmLogin.branchId); userBLL u = uDAL.GetIDFromUsername(username, branchId); transaction.added_by = u.id; transaction.transactionDetails = transactionDT; //Lets Create a Boolean Variable and set its value to false bool success = false; //Actual Code to Insert Transaction And Transaction Details using (TransactionScope scope = new TransactionScope()) { int transactionID = -1; //Create aboolean value and insert transaction bool w = tDAL.Insert_Transaction(transaction, out transactionID); //Use for loop to insert Transaction Details for (int i = 0; i < transactionDT.Rows.Count; i++) { //Get all the details of the product transactionDetailBLL transactionDetail = new transactionDetailBLL(); //Get the Product name and convert it to id string ProductName = transactionDT.Rows[i][0].ToString(); productsBLL p = pDAL.GetProductIDFromName(ProductName, frmLogin.branchId); transactionDetail.product_id = p.id; transactionDetail.rate = decimal.Parse(transactionDT.Rows[i][1].ToString()); transactionDetail.qty = decimal.Parse(transactionDT.Rows[i][2].ToString()); transactionDetail.total = Math.Round(decimal.Parse(transactionDT.Rows[i][3].ToString()), 2); transactionDetail.dea_cust_id = dc.id; transactionDetail.added_date = DateTime.Now; transactionDetail.added_by = u.id; //Here Increase or Decrease Product Quantity based on Purchase or sales string transactionType = lblTop.Text; //Lets check whether we are on Purchase or Sales bool x = false; if (transactionType == "Purchase") { //Increase the Product x = pDAL.IncreaseProduct(transactionDetail.product_id, transactionDetail.qty, frmLogin.branchId); } else if (transactionType == "Sales") { //Decrease the Product Quntiyt x = pDAL.DecreaseProduct(transactionDetail.product_id, transactionDetail.qty, frmLogin.branchId); } //Insert Transaction Details inside the database bool y = tdDAL.InsertTransactionDetail(transactionDetail); success = w && x && y; } if (success == true) { //Transaction Complete scope.Complete(); //Code to Print Bill DGVPrinter printer = new DGVPrinter(); printer.Title = "\r\n\r\n\r\n Falcon Soft PVT. LTD. \r\n\r\n"; printer.SubTitle = "Islamabad, Pakistan \r\n Phone: 03043487853 \r\n\r\n"; printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip; printer.PageNumbers = true; printer.PageNumberInHeader = false; printer.PorportionalColumns = true; printer.HeaderCellAlignment = StringAlignment.Near; printer.Footer = "Discount: " + txtDiscount.Text + "% \r\n" + "VAT: " + txtVat.Text + "% \r\n" + "Grand Total: " + txtGrandTotal.Text + "\r\n\r\n" + "Thank you for doing business with us."; printer.FooterSpacing = 15; printer.PrintDataGridView(dgvAddedProducts); MessageBox.Show("Transaction Completed Sucessfully"); //Celar the Data Grid View and Clear all the TExtboxes dgvAddedProducts.DataSource = null; dgvAddedProducts.Rows.Clear(); txtSearch.Text = ""; txtName.Text = ""; txtEmail.Text = ""; txtContact.Text = ""; txtAddress.Text = ""; txtSearchProduct.Text = ""; txtProductName.Text = ""; txtInventory.Text = "0"; txtRate.Text = "0"; TxtQty.Text = "0"; txtSubTotal.Text = "0"; txtDiscount.Text = "0"; txtVat.Text = "0"; txtGrandTotal.Text = "0"; txtPaidAmount.Text = "0"; txtReturnAmount.Text = "0"; } else { //Transaction Failed MessageBox.Show("Transaction Failed"); } } }