public void insertSales(DataGridView gv, string proIDgv, string quantGV, int doneBy, DateTime dt, float tAmount, float tDiscount, float aGiven, float aReturned, string payType) { try { using (TransactionScope sc = new TransactionScope()) { SqlCommand cmd = new SqlCommand("st_insertSales", MainClass.con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@done", doneBy); cmd.Parameters.AddWithValue("@date", dt); cmd.Parameters.AddWithValue("@totamt", tAmount); cmd.Parameters.AddWithValue("@totdis", tDiscount); cmd.Parameters.AddWithValue("@given", aGiven); cmd.Parameters.AddWithValue("@return", aReturned); if (payType == "Cash") { cmd.Parameters.AddWithValue("@pyType", 0); } else if (payType == "Debit Card ") { cmd.Parameters.AddWithValue("@pyType", 1); } else if (payType == "Credit Card") { cmd.Parameters.AddWithValue("@pyType", 2); } MainClass.con.Open(); salCount = cmd.ExecuteNonQuery(); if (salCount > 0) { SqlCommand cmd2 = new SqlCommand("st_getSalesID", MainClass.con); cmd2.CommandType = CommandType.StoredProcedure; salesID = Convert.ToInt64(cmd2.ExecuteScalar()); foreach (DataGridViewRow row in gv.Rows) { SqlCommand cmd3 = new SqlCommand("st_insertSalesDetails", MainClass.con); cmd3.CommandType = CommandType.StoredProcedure; cmd3.Parameters.AddWithValue("@salID", salesID); cmd3.Parameters.AddWithValue("@proID", Convert.ToInt64(row.Cells["proIDgv"].Value.ToString())); cmd3.Parameters.AddWithValue("@quan", Convert.ToInt64(row.Cells["quantGV"].Value.ToString())); cmd3.ExecuteNonQuery(); int stockofProduct = Convert.ToInt32(r.getProductQuantityWithoutConnection(Convert.ToInt64(row.Cells["proIDgv"].Value.ToString()))); int currentQuanofProduct = Convert.ToInt32(row.Cells["quantGV"].Value.ToString()); int finalProductQuantity = stockofProduct - currentQuanofProduct; u.updateStockWithOutConnction(Convert.ToInt64(row.Cells["proIDgv"].Value.ToString()), finalProductQuantity); } } MainClass.con.Close(); MainClass.showMG("Sales Succesfull", "success", "success"); sc.Complete(); } } catch (Exception) { MainClass.con.Close(); throw; } }