public override void save_button_Click(object sender, EventArgs e) { if (purchase_dataGridView.Rows.Count == 0 || purchase_dataGridView.Rows.Count < 0) { MainClass.ShowMsg("Please enter all required information.", "error"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); } else { MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); try { using (TransactionScope sc = new TransactionScope()) { int compare, compareTO; here: for (int i = 0; i < purchase_dataGridView.Rows.Count; ++i) { compare = Convert.ToInt32(purchase_dataGridView.Rows[i].Cells["productIDGV"].Value); for (int j = i + 1; j < purchase_dataGridView.Rows.Count; ++j) { compareTO = Convert.ToInt32(purchase_dataGridView.Rows[j].Cells["productIDGV"].Value); if (compare == compareTO) { if (correctIT == false) { DialogResult dr = MessageBox.Show("You added a duplicate product which is not allowed.\n\nDo you want to remove that duplicate product and add its quantity?", "Duplicate product(s) encountered!", MessageBoxButtons.YesNo, MessageBoxIcon.Error); if (DialogResult.Yes == dr) { correctIT = true; goto here; } else { correctIT = false; throw new Exception("Duplicate products are not allowed!"); } } else { Int32 originalproductTOTAL, originalproductVALUE, dupliactedproductVALUE, dupliactedproductTOTAL; originalproductVALUE = Convert.ToInt32(purchase_dataGridView.Rows[i].Cells["productquantityGV"].Value); dupliactedproductVALUE = Convert.ToInt32(purchase_dataGridView.Rows[j].Cells["productquantityGV"].Value); originalproductTOTAL = Convert.ToInt32(purchase_dataGridView.Rows[i].Cells["totalGV"].Value); dupliactedproductTOTAL = Convert.ToInt32(purchase_dataGridView.Rows[j].Cells["totalGV"].Value); originalproductVALUE += dupliactedproductVALUE; originalproductTOTAL += dupliactedproductTOTAL; purchase_dataGridView.Rows[i].Cells["productquantityGV"].Value = originalproductVALUE; purchase_dataGridView.Rows[i].Cells["totalGV"].Value = originalproductTOTAL; int beforeREMOVE = purchase_dataGridView.Rows.Count; int afterREMOVE; purchase_dataGridView.Rows.Remove(purchase_dataGridView.Rows[j]); afterREMOVE = purchase_dataGridView.Rows.Count; if (afterREMOVE < beforeREMOVE) { MainClass.ShowMsg("Operation successfull.", "Success"); } else { throw new Exception("An error occured during removing duplicated products!"); } } } } } SQL_TASKS ss = new SQL_TASKS(); foreach (DataGridViewRow row in purchase_dataGridView.Rows) { //inserting purchase invoice purchaseinvoiceID_FROM_SQL_TASKS = ss.InsertPurchaseinvoice(DateTime.Today, LoginCodeClass.USERID, Convert.ToInt32(row.Cells["supplierIDGV"].Value)); //inserting purchase invoice details count += ss.insertPurchaseinvoicedetails(purchaseinvoiceID_FROM_SQL_TASKS, Convert.ToInt32(row.Cells["productIDGV"].Value.ToString()), Convert.ToInt32(row.Cells["productquantityGV"].Value.ToString()), Convert.ToSingle(row.Cells["totalGV"].Value.ToString())); SqlConnection sql_con = new SqlConnection(MainClass.connection()); SqlCommand cmd = new SqlCommand(); cmd.Connection = sql_con; cmd.CommandType = CommandType.Text; cmd.CommandText = "select pp_prodID from ProductPrice where pp_ProductBarcode='" + row.Cells["productbarcodeGV"].Value.ToString() + "' "; sql_con.Open(); object check = cmd.ExecuteScalar(); sql_con.Close(); if (check == null) //product not present { Hashtable ht1 = new Hashtable(); ht1.Add("@prodID", row.Cells["productIDGV"].Value.ToString()); ht1.Add("@bp", Convert.ToSingle(row.Cells["totalGV"].Value.ToString())); ht1.Add("@sp", DBNull.Value); ht1.Add("@buyingdate", DateTime.Today); ht1.Add("@profitper", DBNull.Value); ht1.Add("@discount", DBNull.Value); ht1.Add("@productbarcode", row.Cells["productbarcodeGV"].Value.ToString()); // inserting product price: if (SQL_TASKS.insert_update_delete("st_insertPRODUCTPRICE", ht1) > 0) { success = true; } else { success = false; } } else //product present { Hashtable ht2 = new Hashtable(); ht2.Add("@prodID", row.Cells["productIDGV"].Value.ToString()); cmd.CommandText = "select pp_buyingPrice from ProductPrice where pp_prodID='" + row.Cells["productIDGV"].Value.ToString() + "' "; sql_con.Open(); Int64 ExistingProductBuyingPrice = Convert.ToInt64(cmd.ExecuteScalar()); sql_con.Close(); Int64 finalp = Convert.ToInt64(row.Cells["totalGV"].Value) + ExistingProductBuyingPrice; Hashtable htx = new Hashtable(); htx.Add("@prodID", row.Cells["productIDGV"].Value.ToString()); htx.Add("@bp", finalp); if (SQL_TASKS.insert_update_delete("st_updatePRODUCTPRICEforPURCHASEINVOICE", htx) > 0) { success = true; } else { success = false; } } int q; object stockcount; bool DoesExist = SQL_TASKS.IfProductExist(Convert.ToInt64(row.Cells["productIDGV"].Value.ToString())); if (DoesExist) //if product does exist { //update stock stockcount = SQL_TASKS.getProductQuantityfromSTOCK(Convert.ToInt32(row.Cells["productIDGV"].Value.ToString())); q = Convert.ToInt32(stockcount); q += Convert.ToInt32(row.Cells["productquantityGV"].Value.ToString()); Hashtable hta = new Hashtable(); hta.Add("@prodID", Convert.ToInt32(row.Cells["productIDGV"].Value.ToString())); hta.Add("@quan", q); //updating stock: if (SQL_TASKS.insert_update_delete("st_updateSTOCK", hta) > 0) { success = true; } } else //if product does not exist { //insert Hashtable htb = new Hashtable(); htb.Add("@prodID", Convert.ToInt32(row.Cells["productIDGV"].Value.ToString())); htb.Add("@quan", Convert.ToInt32(row.Cells["productquantityGV"].Value.ToString())); // inserting stock: if (SQL_TASKS.insert_update_delete("st_insertSTOCK", htb) > 0) { success = true; } else { success = false; } } } if (count > 0 && success == true) { MainClass.ShowMsg("Purchase Invoice created successfully.", "Success"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); purchase_dataGridView.Rows.Clear(); gross_total_price_label.Text = ""; } else { MainClass.ShowMsg("Unable to create Purchase Invoice", "Error"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); purchase_dataGridView.Rows.Clear(); gross_total_price_label.Text = ""; } sc.Complete(); } } catch (Exception ex) { MainClass.ShowMsg(ex.Message, "Error"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); } } }