private void btn_save_Click(object sender, EventArgs e) { try { ModCommonClasses.con = new SqlConnection(ModCS.cs); ModCommonClasses.con.Open(); string ct = "select Invoiceno from tbl_salemaster where Invoiceno=@d1"; ModCommonClasses.cmd = new SqlCommand(ct); ModCommonClasses.cmd.Parameters.AddWithValue("@d1", txt_invoice_no.Text); ModCommonClasses.cmd.Connection = ModCommonClasses.con; ModCommonClasses.rdr = ModCommonClasses.cmd.ExecuteReader(); if (ModCommonClasses.rdr.Read()) { MessageBox.Show("Invoice No. Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txt_invoice_no.Text = ""; txt_invoice_no.Focus(); if ((ModCommonClasses.rdr != null)) { ModCommonClasses.rdr.Close(); } return; } ModCommonClasses.con.Close(); ModCommonClasses.con = new SqlConnection(ModCS.cs); ModCommonClasses.con.Open(); string cb = "insert into tbl_salemaster(Inv_ID,Invoiceno,Date,Salesman,Saletype,CustomerID,Customer,Grandtotal,Discount,Discountvalue,Tax,Taxvalue,Netamount,Oldbalance,Newbalance,Receivedamount,Balance,Comments) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17,@d18)"; ModCommonClasses.cmd = new SqlCommand(cb); ModCommonClasses.cmd.Parameters.AddWithValue("@d1", txtinvoice.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d2", txt_invoice_no.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d3", dateTimePicker1.Value.ToString("MM/dd/yyyy")); ModCommonClasses.cmd.Parameters.AddWithValue("@d4", combo_salesman.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d5", combo_bil_type.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d6", txtCus_ID.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d7", txtCustomerName.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d8", txt_total_amount.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d9", combo_DiscountType.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d10", txt_dis_amount.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d11", combo_TaxType.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d12", txt_tax_amount.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d13", txt_Total.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d14", txt_old_bal.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d15", txt_new_bal.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d16", txt_rec_amount.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d17", txt_dues.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d18", txt_comment.Text); ModCommonClasses.cmd.Connection = ModCommonClasses.con; ModCommonClasses.cmd.ExecuteNonQuery(); ModCommonClasses.con.Close(); ModCommonClasses.con = new SqlConnection(ModCS.cs); ModCommonClasses.con.Open(); string cb1 = "insert into tbl_Temp_sale_product(InvoiceID,Productcode,Barcode,Invoiceno,Date,Customer,Productname,Price,Qty,Typeofunit,Total) VALUES (@sId,@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10)"; ModCommonClasses.cmd = new SqlCommand(cb1); ModCommonClasses.cmd.Connection = ModCommonClasses.con; // Prepare command for repeated execution ModCommonClasses.cmd.Prepare(); // Data to be inserted foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) { ModCommonClasses.cmd.Parameters.AddWithValue("@sId", txtinvoice.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d1", row.Cells[0].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d2", row.Cells[1].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d3", txt_invoice_no.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d4", dateTimePicker1.Value.ToString("MM/dd/yyyy")); ModCommonClasses.cmd.Parameters.AddWithValue("@d5", txtCustomerName.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d6", row.Cells[2].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d7", row.Cells[3].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d8", row.Cells[4].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d9", row.Cells[5].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d10", row.Cells[6].Value); ModCommonClasses.cmd.ExecuteNonQuery(); ModCommonClasses.cmd.Parameters.Clear(); } } ModCommonClasses.con.Close(); ModCommonClasses.con = new SqlConnection(ModCS.cs); ModCommonClasses.con.Open(); string cb2 = "insert into tbl_sale_pro(InvoiceID,Productcode,Barcode,Invoiceno,Date,Customer,Productname,Price,Qty,Typeofunit,Total) VALUES (@sId,@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10)"; ModCommonClasses.cmd = new SqlCommand(cb2); ModCommonClasses.cmd.Connection = ModCommonClasses.con; // Prepare command for repeated execution ModCommonClasses.cmd.Prepare(); // Data to be inserted foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) { ModCommonClasses.cmd.Parameters.AddWithValue("@sId", txtinvoice.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d1", row.Cells[0].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d2", row.Cells[1].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d3", txt_invoice_no.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d4", dateTimePicker1.Value.ToString("MM/dd/yyyy")); ModCommonClasses.cmd.Parameters.AddWithValue("@d5", txtCustomerName.Text); ModCommonClasses.cmd.Parameters.AddWithValue("@d6", row.Cells[2].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d7", row.Cells[3].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d8", row.Cells[4].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d9", row.Cells[5].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d10", row.Cells[6].Value); ModCommonClasses.cmd.ExecuteNonQuery(); ModCommonClasses.cmd.Parameters.Clear(); } } ModCommonClasses.con.Close(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) { ModCommonClasses.con = new SqlConnection(ModCS.cs); ModCommonClasses.con.Open(); string ctx = "select Productcode from Temp_Stock where Productcode=@d1 and Barcodeno=@d2"; ModCommonClasses.cmd = new SqlCommand(ctx); ModCommonClasses.cmd.Connection = ModCommonClasses.con; ModCommonClasses.cmd.Parameters.AddWithValue("@d1", row.Cells[0].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d2", row.Cells[1].Value); ModCommonClasses.rdr = ModCommonClasses.cmd.ExecuteReader(); if ((ModCommonClasses.rdr.Read())) { ModCommonClasses.con = new SqlConnection(ModCS.cs); ModCommonClasses.con.Open(); string cb3 = "Update Temp_Stock set Quantity = Quantity - " + row.Cells[4].Value + " where Productcode=@d1 and Barcodeno=@d2"; ModCommonClasses.cmd = new SqlCommand(cb3); ModCommonClasses.cmd.Connection = ModCommonClasses.con; ModCommonClasses.cmd.Parameters.AddWithValue("@d1", row.Cells[0].Value); ModCommonClasses.cmd.Parameters.AddWithValue("@d2", row.Cells[1].Value); ModCommonClasses.cmd.ExecuteReader(); ModCommonClasses.con.Close(); } } } if (combo_bil_type.SelectedIndex == 1) { ModFunc.CustomerLedgerSave(dateTimePicker1.Text, txtCustomerID.Text, txtCustomerName.Text, "Sale Credit", 0, Convert.ToDecimal(txt_new_bal.Text) - Convert.ToDecimal(txt_old_bal.Text), txt_cus_mob.Text); } if (combo_bil_type.SelectedIndex == 0) { ModFunc.CustomerLedgerSave(dateTimePicker1.Text, txtCustomerID.Text, txtCustomerName.Text, "Sale Credit", 0, Convert.ToDecimal(txt_new_bal.Text) - Convert.ToDecimal(txt_old_bal.Text), txt_cus_mob.Text); ModFunc.CustomerLedgerSave(dateTimePicker1.Text, txtCustomerID.Text, "Cash Account", "Payment", Convert.ToDecimal(txt_rec_amount.Text), 0, txt_cus_mob.Text); } if (combo_bil_type.SelectedIndex == 1) { ModFunc.LedgerSaveInvoice(dateTimePicker1.Text, txtCustomerID.Text, txtCustomerName.Text, "Sale Credit", 0, Convert.ToDecimal(txt_new_bal.Text) - Convert.ToDecimal(txt_old_bal.Text), txt_cus_mob.Text); } if (combo_bil_type.SelectedIndex == 0) { ModFunc.LedgerSaveInvoice(dateTimePicker1.Text, txtCustomerID.Text, txtCustomerName.Text, "Sale Credit", 0, Convert.ToDecimal(txt_new_bal.Text) - Convert.ToDecimal(txt_old_bal.Text), txt_cus_mob.Text); ModFunc.LedgerSaveInvoice(dateTimePicker1.Text, txtCustomerID.Text, "Cash Account", "Payment", Convert.ToDecimal(txt_rec_amount.Text), 0, txt_cus_mob.Text); } printDocument1.Print(); ModFunc.LogFunc(lblUser.Text, "Added the New Sale Having Invoice No. '" + txt_invoice_no.Text + "'"); MessageBox.Show("Successfully Saved", "Stock", MessageBoxButtons.OK, MessageBoxIcon.Information); stockid(); this.ActiveControl = txt_pro_name; txt_pro_name.Focus(); btn_save.Enabled = false; btnnew.Enabled = true; ModCommonClasses.con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }