![或者在保存之前恢复到saleinvoice 或者在保存之前恢复到saleinvoice]()
本文介绍了如何将数据从一个C#windowsform更新为3个SQL服务器表,或者在保存之前恢复到saleinvoice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想知道在保存之前如何准确地获得此销售发票。我在多个表格中保存数据,但我没有找到在所有表格中更新的方法,下面是完整的销售表格代码 //////////// 使用System; 使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Linq; 使用System.Text; //使用System.Threading .Tasks; 使用System.Windows.Forms; 使用System.Data.SqlClient; 名称空间Windows { 公共部分类FormSale:表格 { // SqlConnection conn = new SqlConnection( Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); SqlCommand cmd; SqlCommandBuilder cmdbuilder; SqlDataAdapter sda; DataTable dt; public FormSale() { InitializeCompo nent(); } private void txtTransporter_KeyUp(object sender,KeyEventArgs e) { // if( e.KeyCode == Keys.Enter) // { // this.SelectNextControl(txtTransporter,true,true,true,true); //} } private void btninsert_KeyUp (对象发送者,KeyEventArgs e) { } private void FormSale_Load(object sender,EventArgs e) { // TODO:这行代码将数据加载到'windowsDbDataSet.SaleItem中'表。您可以根据需要移动或删除它。 //this.saleItemTableAdapter.Fill(this.windowsDbDataSet.SaleItem); // autoFillFields(); fillcBoxTransporter(); fillcBoxPartyName(); fillcBoxICode(); fillcBoxLocation(); fillcBoxItemParty(); fillcBoxCashParty(); //this.ActiveControl = cBoxTransporter; //cBoxPartyName.Focus(); // TODO:这行代码将数据加载到'windowsDbDataSet.ItemTypes中'表。您可以根据需要移动或删除它。 //this.itemTypesTableAdapter.Fill(this.windowsDbDataSet.ItemTypes); autoVoucherId(); autoSaleId(); autoBillNo(); } public void autoVoucherId() {//生成自动Saleid数字 SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open(); cmd = new SqlCommand(SELECT max(VoucherId)+1 FROM优惠券,conn); SqlDataReader sdr = cmd.ExecuteReader(); if(sdr.HasRows) { while(sdr.Read()) { txtVoucherNo.Text = sdr [0] .ToString(); if(txtVoucherNo.Text ==) { txtVoucherNo.Text =1; } } } else { txtVoucherNo.Text =1; } conn.Close(); } public void autoSaleId() { //生成自动Saleid数字 SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open (); cmd = new SqlCommand(SELECT max(Id)+1 FROM SaleMaster,conn); SqlDataReader sdr = cmd.ExecuteReader(); if(sdr.HasRows) { while(sdr.Read()) { txtInvoiceId.Text = sdr [0] .ToString(); if(txtInvoiceId.Text ==) { txtInvoiceId。 Text =1; } } } 其他 { txtInvoiceId.Text =1; } conn.Close(); } public void autoBillNo() {//生成自动Saleid编号 SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalogue = WindowsDb; Integrated Security = True); conn.Open(); cmd = new SqlCommand(SELECT max(BillNo)+1 FROM SaleMaster,conn ); SqlDataReader sdr = cmd.ExecuteReader(); if(sdr.HasRows) { while(sdr.Read()) { txtGrandSale.Text = sdr [0] .ToString(); if(txtGrandSale。文字==) { txtGrandSale.Text =1; } } } 其他 { txtGrandSale.Text =1; } conn.Close(); } private void txtDetail_TextChanged(object sender,EventArgs e) { } private void txtQty_KeyPress(object sender,KeyPressEventArgs e) { // if(!char.IsNumber(e.KeyChar)&&!char.IsControl(e.KeyChar)) // { // e.Handled = true; //} } private void txtSaleInvoice_TextChanged(object sender,EventArgs e) { } private void txtBagsAmount_TextChanged(object sender,EventArgs e) { } private void txtBagsRate_TextChanged(object sender,EventArgs e) { 尝试 { txtBagsAmount.Text =(int.Parse(txtQty.Text)* int.Parse( txtBagsRate.Text))。ToString(); } catch { } } private void txtTotalAmount_TextChanged_1(对象发件人,EventArgs e) { } //私人vo id txtPacking_TextChanged_1(对象发件人,EventArgs e) // { //} private void dataGridVSale_CellContentClick(object sender,DataGridViewCellEventArgs e) { } private void txtQty_TextChanged_1(对象发送者,EventArgs e) { 尝试 { txtTotalWeight.Text =(float.Parse(txtPacking.Text)* float.Parse(txtQty.Text))。ToString(); } catch { } } private void btnExit_Click(object sender,EventArgs e) { this.Close(); } private void txtRate_TextChanged_1(对象发件人,EventArgs e) { try { txtTotalAmount.Text =(float.Parse(txtTotalWeight.Text)/ int.Parse(txtUnit.Text) * int.Parse(txtRate.Text))。ToString(); } catch { } } private void txtTotalWeight_TextChanged(object sender,EventArgs e) { 尝试 { txtPacking.Text =(float.Parse(txtTotalWeight.Text)/ float.Parse(txtQty.Text )。ToString(); } catch { } 尝试 { txtTotalAmount.Text =(float.Parse(txtTotalWeight.Text)/ int .Parse(txtUnit.Text)* int.Parse(txtRate.Text))。ToString(); } catch { } } private void txtUnit_TextChanged_1(对象发件人,EventArgs e) { 尝试 { txtTotalAmount.Text =(int.Parse(txtTotalWeight.Text)/ int.Parse(txtUnit.Text)* int.Parse(txtRate.Text))。ToString(); } catch { } } private void btnSave_Click(object sender,EventArgs e) { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); // string col1 = dataGridViewSale [0,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); // string col2 = dataGridViewSale [1,dataGridViewSale.CurrentC ell.RowIndex] .Value.ToString(); // string col3 = dataGridViewSale [2,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); / / string col4 = dataGridViewSale [3,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); // string col5 = dataGridViewSale [4,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString() ; //字符串col6 = dataGridViewSale [5,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); //字符串col7 = dataGridViewSale [6,dataGridViewSale。 CurrentCell.RowIndex] .Value.ToString(); // string col8 = dataGridViewSale [7,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); / / string col9 = dataGridViewSale [8,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); // string col10 = dataGridViewSale [9,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString() ; //字符串col11 = dataGridViewSale [10,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); // string col12 = dataGridViewSale [11,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); // string col13 = dataGridViewSale [12,dataGridViewSale.CurrentCell.RowIndex] .Value.ToString(); int save = int.Parse(lblTotalAmount.Text); lblTotalAmount.Text = save.ToString(); if(save> 0) { for(int i = 0;我< dataGridViewSale.Rows.Count - 1; i ++) { conn.Open(); string query =INSERT INTO SaleItem(SaleId,BillNo,Date,PartyName,ItemCode ,ItemName,Location,Qty,Packing,TotalWeight,Unit,Rate,Amount,Lot,BagsRate,BagsAmountEstimate,ItemParty)VALUES('+ txtInvoiceId.Text +','+ txtGrandSale.Text +','+ dateTimeSale.Text +',N'+ txtPartyName.Text +','+ dataGridViewSale.Rows [i] .Cells [1] .Value +',N'+ dataGridViewSale.Rows [i] .Cells [2] .Value +',N'+ dataGridViewSale.Rows [i] .Cells [3] .Value +','+ dataGridViewSale.Rows [i] .Cells [4] .Value +', '+ dataGridViewSale.Rows [i] .Cells [5] .Value +','+ dataGridViewSale.Rows [i] .Cells [6] .Value +','+ dataGridViewSale.Rows [i]。单元格[7] .Value +','+ dataGridViewSale.Rows [i] .Cells [8] .Value +','+ dataGridViewSale.Rows [i] .Cells [9] .Value +', N'+ dataGridViewSale.Rows [i] .Cells [10] .Value +','+ dataGridViewSale.Rows [i] .Cel ls [11] .Value +','+ dataGridViewSale.Rows [i] .Cells [12] .Value +',N'+ dataGridViewSale.Rows [i] .Cells [13] .Value +' ); SqlDataAdapter sda = new SqlDataAdapter(query,conn); //اسویلیوسےپہلےلازمیلکھناہےجواردومیںںواوپروالیلائنکوغورسےدیکھیںN sda.SelectCommand.ExecuteNonQuery(); conn.Close(); } conn.Open(); string query1 =INSERT INTO SaleMaster(VoucherId,BillNo,SaleDate,Transporter,PartyName,PartyNameText,Details,CashParty,Received,Remaining ,TotalBill)VALUES('+ txtVoucherNo.Text +','+ txtGrandSale.Text +','+ dateTimeSale.Text +',N'+ cBoxTransporter.Text +',N'+ cBoxPartyName.Text +',N'+ txtPartyName.Text +',N'+ txtSDetail.Tex t +',N'+ cBoxCashParty.Text +','+ txtReceived.Text +','+ lblRemaining.Text +','+ lblTotalAmount.Text +'); SqlDataAdapter sda1 = new SqlDataAdapter(query1,conn); sda1.SelectCommand.ExecuteNonQuery(); conn.Close(); MessageBox.Show(已保存); for(int i = 0;我< dataGridViewSale.Rows.Count - 1; i ++) { conn.Open(); string query =INSERT INTO Vouchers(SaleId,BillNo,Date,PartyName,Debit ,详情)VALUES('+ txtInvoiceId.Text +','+ txtGrandSale.Text +','+ dateTimeSale.Text +',N'+ cBoxPartyName.Text +','+ dataGridViewSale .Rows [i] .Cells [9] .Value +',N'+ txtSDetail.Text +'); SqlDataAdapter sda = new SqlDataAdapter(query,conn); //اسویلیوسےپالالجواردومیںںواوپروالیلائنکوغورسےدیکھیںN sda.SelectCommand.ExecuteNonQuery(); conn.Close(); // SqlCommand cmd = new SqlCommand(INSERT INTO Vouchers(SaleId,BillNo,Date,PartyName,Debit,Details)值(@ SaleId,@ BillNo @日,@ PartyName,@借记卡,@详细),conn); //cmd.Parameters.AddWithValue(\"@SaleId,txtInvoiceId.Text); //cmd.Parameters.AddWithValue(\"@ BillNo,txtGrandSale.Text); //cmd.Parameters.AddWithValue(\"@Date,dateTimeSale.Text); //cmd.Parameters.AddWithValue( @PartyName,cBoxPartyName.Text); //cmd.Parameters.AddWithValue(\"@Debit,dataGridViewSale.Rows [i] .Cells [9] .Value); //cmd.Parameters.AddWithValue(\"@Details,txtSDetail.Text); //conn.Open(); //cmd.ExecuteNonQuery(); //conn.Close(); //MessageBox.Show(已成功添加!); } for(int i = 0; i< dataGridViewSale.Rows。数 - 1; i ++) { conn.Open(); string query =INSERT INTO Vouchers(SaleId,BillNo,Date ,PartyName,Credit,Details)VALUES('+ txtInvoiceId.Text +','+ txtGrandSale.Text +','+ dateTimeSale.Text +',N'+ dataGridViewSale.Rows [i]。 Cells [13] .Value +','+ dataGridViewSale.Rows [i] .Cells [9] .Value +',N'+ txtSDetail.Text +'); SqlDataAdapter sda = new SqlDataAdapter(query,conn); // @ووللووے غورسےدیکھیںN sda.SelectCommand.ExecuteNonQuery(); conn.Close(); } conn.Open(); string query3 =INSERT INTO Vouchers(SaleId,BillNo,Date,PartyName,Debit,Details)VALUES('+ txtInvoiceId.Text +','+ txtGrandSale.Text +','+ dateTimeSale.Text +',N'+ cBoxCashParty.Text +','+ txtReceived.Text +',N'+ txtSDetail .Text +'); SqlDataAdapter sda3 = new SqlDataAdapter(query3,conn); //اسویلیوسےپہلےلازمیلکھناہےجواردومیںںواوپروالیلائنکوغورسےدیکھیں N $ / $ sda3.SelectCommand.ExecuteNonQuery(); conn.Close(); conn.Open (); string query2 =INSERT INTO Vouche rs(SaleId,BillNo,Date,PartyName,Credit,Details)VALUES('+ txtInvoiceId.Text +','+ txtGrandSale.Text +','+ dateTimeSale.Text +',N'+ cBoxPartyName.Text +','+ txtReceived.Text +',N'+ txtSDetail.Text +'); SqlDataAdapter sda2 = new SqlDataAdapter(query2,conn); //اسویلیوسےپالالجواردومیںںواوپروالیلائنکوغورسےدیکھیںN sda2.SelectCommand.ExecuteNonQuery(); conn.Close(); this.dataGridViewSale.DataSource = null; this.dataGridViewSale.Rows.Clear(); } 其他 { MessageBox.Show(无账单金额); } } //试试 // { //} // catch // { / /} private void txtReceived_TextChanged(object sender,EventArgs e) { 尝试 { lblRemaining.Text =(int.Parse(lblTotalAmount.Text) - int.Parse(txtReceived.Text ))。ToString(); } catch { } } private void txtPacking_TextChanged(object sender,EventArgs e) { 尝试 { txtTotalWeight.Text =(float.Parse(txtPacking.Text)* float.Parse(txtQty.Text )。ToString(); } catch { } } private void btnNew_Click (对象发送者,EventArgs e) { txtSDetail.Clear(); this.Close(); FormSale sale = new FormSale(); // if((sale.lblTotalAmount.Text)= > 0) // { //} sale.Show(); } private void btnEdit_Click(object sender,EventArgs e) { FormSaleEdit edit = new FormSaleEdit(); edit.Show(); // FormSaleEdit saleEdit = new FormSaleEdit(); //saleEdit.Show(); } private void txtGrandSale_TextChanged(object sender,EventArgs e) { } public void SaleNo(string saleNumber) { saleNumber = txtGrandSale.Text; } //私人void btnPrevious_Click(object sender,EventArgs e) // { // SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security =真); // for(int i = 0;我< dataGridViewSale.Rows.Count - 1;我 - ) // { // conn.Open(); //字符串查询=SELECT * FROM SaleItem where BillNO ='+ txtGrandSale.Text +'; // sda = new SqlDataAdapter(query,conn); // dt = new DataTable(); // sda.Fill(dt); // dgvSale.DataSource = dt; // conn.Close(); //} //} void fillcBoxTransporter() { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open(); sda = new SqlDataAdapter(select * from Transporter,conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxTransporter.DataSource = dt; cBoxTransporter.DisplayMember =Name; conn.Close(); } void fillcBoxPartyName() { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open( ); sda = new SqlDataAdapter(选择Party的PartyName,conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxPartyName.DataSource = dt; cBoxPartyName.DisplayMember =PartyName; conn.Close(); } void fillcBoxICode() { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True ); conn.Open(); sda = new SqlDataAdapter(select * from Items,conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxICode.DataSource = dt; cBoxICode.DisplayMember =ItemCode; conn.Close(); } void fillcBoxLocation( ) { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open(); sda = new SqlDataAdapter(select * from Locations,conn); DataTable dt = new DataTable (); sda.Fill(dt); cBoxLocation.DataSource = dt; cBoxLocation。 DisplayMember =Location; conn.Close(); } void fillcBoxItemParty() { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open(); sda = new SqlDataAdapter(selec t *来自缔约方,conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxItemParty.DataSource = dt; cBoxItemParty.DisplayMember =PartyName; conn.Close(); } void fillcBoxCashParty() { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open(); sda = new SqlDataAdapter(select * from Parties where Id = 2,conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxCashParty.DataSource = dt; cBoxCashParty.DisplayMember =PartyName; conn.Close( ); } public void autoFillFie lds() {// conn = new SqlConnection(); // string query =select * from items where ItemCode ='+ cBoxICode.Text +' ; // cmd = new SqlCommand(query,conn); // SqlDataReader sdr; //试试 // { // conn.Open(); // sdr = cmd.ExecuteReader(); / / while(sdr.Read()) // { // string iCode = sdr.GetInt32(1).ToString(); // string iParty = sdr.GetString(2); // string iName = sdr.GetString(3); // string iPacking = sdr.GetInt32 (4).ToString(); // string iUnit = sdr.GetInt32(5).ToString(); // cBoxICode.Text = iCode; // cBoxItemParty.Text = iParty; // txtItemName.Text = iName; // txtPacking.Text = iPacking; // txtUnit.Text = iUnit; // } // conn.Close(); //} // catch // { //} // conn.Open(); //字符串查询=select * from Items where ItemCode ='+ cBoxICode.SelectedItem.ToString()+'; // sda = new SqlDataAdapter (query,conn); //sda.SelectCommand.ExecuteNonQuery(); // dt = new DataTable(); / / sda = new SqlDataAdapter(cmd); // sda.Fill(dt); // foreach(dt.Rows中的DataRow dr) // { // cBoxICode.Text = dr [ItemCode]。ToString(); // txtItemName.Text = dr [ItemName ] .ToString(); // txtPacking.Text = dr [Packing]。ToString(); // txtUnit.Text = dr [Unit ] .ToString(); // cBoxItemParty.Text = dr [ItemParty]。ToString(); //} // conn.Close(); } private void cBoxICode_Selec tedIndexChanged(object sender,EventArgs e) { SqlConnection conn = new SqlConnection(Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True); conn.Open(); cmd = new SqlCommand(SELECT * FROM Items where ItemCode ='+ cBoxICode.Text +',conn); cmd.ExecuteNonQuery(); SqlDataReader博士; dr = cmd.ExecuteReader(); while(dr.Read()) { string iName =(string)dr [ItemName]。ToString(); txtItemName.Text = iName; string iPacking =(string)dr [Packing]。ToString(); txtPacking.Text = iPacking; string iUnit =(string)dr [Unit]。ToString(); txtUnit.Text = iUnit; string iParty =(字符串)dr [ItemParty]。ToString(); cBoxItemParty.Text = iParty; } conn.Close(); } private void cBoxICode_KeyDown(object sender,KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { if(string.IsNullOrEmpty(cBoxICode.Text)) { ((System.Windows.Forms.ComboBox)sender).DroppedDown = true; } else { cBoxLocation.Focus(); } } //否则if(e.KeyCode == Keys.F1) // { // cBoxICode.Focus(); //((System.Windows.Forms.ComboBox)sender).DroppedDown = true; //} else if(e.KeyCode == Keys.Insert) { txtReceived。焦点(); } } private void cBoxTransporter_KeyDown(object sender,KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { cBoxPartyName.Focus(); } else if(e.KeyCode == Keys.F1) { cBoxTransporter.Focus() ; ((System.Windows.Forms.ComboBox)sender).DroppedDown = true; } } $b $b private void cBoxPartyName_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtPartyName.Focus(); } else if (e.KeyCode == Keys.F1) { cBoxPartyName.Focus(); ((System.Windows.Forms.ComboBox)sender).DroppedDown = true; } txtPartyName.Text = cBoxPartyName.Text; } private void txtSDetail_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { cBoxICode.Focus(); } } private void cBoxLocation_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtQty.Focus(); } } private void txtQty_KeyDown(object sender, KeyEventArgs e ) { if (e.KeyCode == Keys.Enter) { txtTotalWeight.Focus(); } } private void txtTotalWeight_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtRate.Focus(); } } private void txtRate_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtMarka.Focus(); } } private void txtMarka_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtBagsRate.Focus(); } } private void btnSave_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnPrint.Focus(); } } private void btninsert_KeyDown_1(object sender, KeyEventArgs e) { //if (e.KeyCode==Keys.Tab) //{ // cBoxICode.Focus(); //} } private void txtBagsRate_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { DataTable dt = new DataTable(); dt.Columns.Add(\"LineId\"); dt.Columns.Add(\"code\"); dt.Columns.Add(\"item\"); dt.Columns.Add(\"location\"); dt.Columns.Add(\"qty\"); dt.Columns.Add(\"packing\"); dt.Columns.Add(\"totalWeight\"); dt.Columns.Add(\"unit\"); dt.Columns.Add(\"rate\"); dt.Columns.Add(\"amount\"); dt.Columns.Add(\"marka\"); dt.Columns.Add(\"bagsRate\"); dt.Columns.Add(\"bagsAmount\"); dt.Columns.Add(\"itemParty\"); DataRow row = dt.NewRow(); row[\"LineId\"] = cBoxICode.Text; row[\"code\"] = cBoxICode.Text; row[\"item\"] = txtItemName.Text; row[\"location\"] = cBoxLocation.Text; row[\"qty\"] = txtQty.Text; row[\"packing\"] = txtPacking.Text; row[\"totalWeight\"] = txtTotalWeight.Text; row[\"unit\"] = txtUnit.Text; row[\"rate\"] = txtRate.Text; row[\"amount\"] = txtTotalAmount.Text; row[\"marka\"] = txtMarka.Text; row[\"bagsRate\"] = txtBagsRate.Text; row[\"bagsAmount\"] =txtBagsAmount.Text; row[\"itemParty\"] = cBoxItemParty.Text; dt.Rows.Add(row); foreach (DataRow Drow in dt.Rows) { int num = dataGridViewSale.Rows.Add(); dataGridViewSale.Rows[num].Cells[0].Value = Drow[\"LineId\"].ToString(); dataGridViewSale.Rows[num].Cells[1].Value = Drow[\"code\"].ToString(); dataGridViewSale.Rows[num].Cells[2].Value = Drow[\"item\"].ToString(); dataGridViewSale.Rows[num].Cells[3].Value = Drow[\"location\"].ToString(); dataGridViewSale.Rows[num].Cells[4].Value = Drow[\"qty\"].ToString(); dataGridViewSale.Rows[num].Cells[5].Value = Drow[\"packing\"].ToString(); dataGridViewSale.Rows[num].Cells[6].Value = Drow[\"totalWeight\"].ToString(); dataGridViewSale.Rows[num].Cells[7].Value = Drow[\"unit\"].ToString(); dataGridViewSale.Rows[num].Cells[8].Value = Drow[\"rate\"].ToString(); dataGridViewSale.Rows[num].Cells[9].Value = Drow[\"amount\"].ToString(); dataGridViewSale.Rows[num].Cells[10].Value = Drow[\"marka\"].ToString(); dataGridViewSale.Rows[num].Cells[11].Value = Drow[\"bagsRate\"].ToString(); dataGridViewSale.Rows[num].Cells[12].Value = Drow[\"bagsAmount\"].ToString(); dataGridViewSale.Rows[num].Cells[13].Value = Drow[\"itemParty\"].ToString(); //cBoxTransporter.Text = \"\"; //cBoxPartyName.Text = \"\"; cBoxICode.Text=\"\"; txtItemName.Clear(); txtQty.Clear(); txtTotalWeight.Clear(); txtPacking.Clear(); txtRate.Clear(); txtTotalAmount.Clear(); //txtReceived.Clear(); try { int A = 0, B = 0; for (A = 0; A < dataGridViewSale.Rows.Count; ++A) { B += Convert.ToInt32(dataGridViewSale.Rows[A].Cells[4].Value); } lblTotalTadad.Text = B.ToString(); int C = 0, D = 0; for (C = 0; C < dataGridViewSale.Rows.Count; ++C) { D += Convert.ToInt32(dataGridViewSale.Rows[C].Cells[12].Value); } lblTotalBardana.Text = D.ToString(); } catch { } } try { int E = 0, F = 0; for (E = 0; E < dataGridViewSale.Rows.Count; ++E) { F += Convert.ToInt32(dataGridViewSale.Rows[E].Cells[9].Value); } lblTotalAmount.Text = F.ToString(); } catch { } cBoxICode.Focus(); } } private void btnPrint_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnNew.Focus(); } } private void btnNew_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { cBoxTransporter.Focus(); } } private void txtReceived_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { btnSave.Focus(); } } private void txtSDetail_KeyDown_1(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { cBoxICode.Focus(); } } private void txtPartyName_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { txtSDetail.Focus(); } } private void btnNext_Click(object sender, EventArgs e) { } private void btnUpdate_Click(object sender, EventArgs e) { //SqlConnection conn = new SqlConnection(\"Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True\"); //conn.Open(); //string query = \"Update Id, VoucherId, BillNo, SaleDate, Transporter,PartyId,PartyName,PartyNameText,Details,CashParty,Received,Remaining,TotalBill, LineId, ItemCode, ItemName, Location, Qty,Packing, TotalWeight, Unit, Rate, Amount, Lot, BagsRate, BagsAmountEstimate,ItemParty,ItemId, LocationId FROM SaleBillView where BillNO=’\" + txtGrandSale.Text + \"’ \"; //sda = new SqlDataAdapter(query, conn); //dt = new DataTable(); //sda.Fill(dt); //dgvSale.DataSource = dt; //conn.Close(); //dbo.SaleMaster.Id, dbo.SaleMaster.VoucherId, dbo.SaleMaster.BillNo, dbo.SaleMaster.SaleDate, dbo.SaleMaster.Transporter, dbo.SaleMaster.PartyId, dbo.SaleMaster.PartyName, // dbo.SaleMaster.PartyNameText, dbo.SaleMaster.Details, dbo.SaleMaster.CashParty, dbo.SaleMaster.Received, dbo.SaleMaster.Remaining, dbo.SaleMaster.TotalBill, dbo.SaleItem.LineId, // dbo.SaleItem.ItemCode, dbo.SaleItem.ItemName, dbo.SaleItem.Location, dbo.SaleItem.Qty, dbo.SaleItem.Packing, dbo.SaleItem.TotalWeight, dbo.SaleItem.Unit, dbo.SaleItem.Rate, dbo.SaleItem.Amount, // dbo.SaleItem.Lot, dbo.SaleItem.BagsRate, dbo.SaleItem.BagsAmountEstimate, dbo.SaleItem.ItemParty, dbo.SaleItem.ItemId, dbo.SaleItem.LocationId } } } What I have tried: the codes above i have tried by different ways.i want to know how to get this sale invoice exactly before i saved it. i save data in multiple tables but i did not find a way to update in all tables, Below full code of Sale Form////////////using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;//using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient;namespace Windows{ public partial class FormSale : Form { //SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); SqlCommand cmd; SqlCommandBuilder cmdbuilder; SqlDataAdapter sda; DataTable dt; public FormSale() { InitializeComponent(); } private void txtTransporter_KeyUp(object sender, KeyEventArgs e) { //if (e.KeyCode==Keys.Enter) //{ // this.SelectNextControl(txtTransporter, true, true, true, true); //} } private void btninsert_KeyUp(object sender, KeyEventArgs e) { } private void FormSale_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'windowsDbDataSet.SaleItem' table. You can move, or remove it, as needed. //this.saleItemTableAdapter.Fill(this.windowsDbDataSet.SaleItem); //autoFillFields(); fillcBoxTransporter(); fillcBoxPartyName(); fillcBoxICode(); fillcBoxLocation(); fillcBoxItemParty(); fillcBoxCashParty(); //this.ActiveControl = cBoxTransporter; //cBoxPartyName.Focus(); // TODO: This line of code loads data into the 'windowsDbDataSet.ItemTypes' table. You can move, or remove it, as needed. //this.itemTypesTableAdapter.Fill(this.windowsDbDataSet.ItemTypes); autoVoucherId(); autoSaleId(); autoBillNo(); } public void autoVoucherId() {// To generate automatic Saleid number SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); cmd = new SqlCommand("SELECT max(VoucherId)+1 FROM Vouchers", conn); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.HasRows) { while (sdr.Read()) { txtVoucherNo.Text = sdr[0].ToString(); if (txtVoucherNo.Text == "") { txtVoucherNo.Text = "1"; } } } else { txtVoucherNo.Text = "1"; } conn.Close(); } public void autoSaleId() {// To generate automatic Saleid number SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); cmd = new SqlCommand("SELECT max(Id)+1 FROM SaleMaster", conn); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.HasRows) { while (sdr.Read()) { txtInvoiceId.Text = sdr[0].ToString(); if (txtInvoiceId.Text == "") { txtInvoiceId.Text = "1"; } } } else { txtInvoiceId.Text = "1"; } conn.Close(); } public void autoBillNo() {// To generate automatic Saleid number SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); cmd = new SqlCommand("SELECT max(BillNo)+1 FROM SaleMaster ", conn); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.HasRows) { while (sdr.Read()) { txtGrandSale.Text = sdr[0].ToString(); if (txtGrandSale.Text == "") { txtGrandSale.Text = "1"; } } } else { txtGrandSale.Text = "1"; } conn.Close(); } private void txtDetail_TextChanged(object sender, EventArgs e) { } private void txtQty_KeyPress(object sender, KeyPressEventArgs e) { // if (!char.IsNumber(e.KeyChar)&&!char.IsControl(e.KeyChar)) // { // e.Handled = true; // } } private void txtSaleInvoice_TextChanged(object sender, EventArgs e) { } private void txtBagsAmount_TextChanged(object sender, EventArgs e) { } private void txtBagsRate_TextChanged(object sender, EventArgs e) { try { txtBagsAmount.Text = (int.Parse(txtQty.Text) * int.Parse(txtBagsRate.Text)).ToString(); } catch { } } private void txtTotalAmount_TextChanged_1(object sender, EventArgs e) { } //private void txtPacking_TextChanged_1(object sender, EventArgs e) //{ //} private void dataGridVSale_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void txtQty_TextChanged_1(object sender, EventArgs e) { try { txtTotalWeight.Text = (float.Parse(txtPacking.Text) * float.Parse(txtQty.Text)).ToString(); } catch { } } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void txtRate_TextChanged_1(object sender, EventArgs e) { try { txtTotalAmount.Text = (float.Parse(txtTotalWeight.Text) / int.Parse(txtUnit.Text) * int.Parse(txtRate.Text)).ToString(); } catch { } } private void txtTotalWeight_TextChanged(object sender, EventArgs e) { try { txtPacking.Text = (float.Parse(txtTotalWeight.Text) / float.Parse(txtQty.Text)).ToString(); } catch { } try { txtTotalAmount.Text = (float.Parse(txtTotalWeight.Text) / int.Parse(txtUnit.Text) * int.Parse(txtRate.Text)).ToString(); } catch { } } private void txtUnit_TextChanged_1(object sender, EventArgs e) { try { txtTotalAmount.Text = (int.Parse(txtTotalWeight.Text) / int.Parse(txtUnit.Text) * int.Parse(txtRate.Text)).ToString(); } catch { } } private void btnSave_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); //string col1 = dataGridViewSale[0, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col2 = dataGridViewSale[1, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col3 = dataGridViewSale[2, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col4 = dataGridViewSale[3, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col5 = dataGridViewSale[4, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col6 = dataGridViewSale[5, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col7 = dataGridViewSale[6, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col8 = dataGridViewSale[7, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col9 = dataGridViewSale[8, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col10 = dataGridViewSale[9, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col11 = dataGridViewSale[10, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col12 = dataGridViewSale[11, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); //string col13 = dataGridViewSale[12, dataGridViewSale.CurrentCell.RowIndex].Value.ToString(); int save = int.Parse(lblTotalAmount.Text); lblTotalAmount.Text=save.ToString(); if (save>0) { for (int i = 0; i < dataGridViewSale.Rows.Count - 1; i++) { conn.Open(); string query = "INSERT INTO SaleItem(SaleId,BillNo,Date,PartyName,ItemCode,ItemName,Location,Qty,Packing,TotalWeight,Unit,Rate,Amount,Lot,BagsRate,BagsAmountEstimate,ItemParty)VALUES('" + txtInvoiceId.Text + "','" + txtGrandSale.Text + "','" + dateTimeSale.Text + "',N'" + txtPartyName.Text + "','" + dataGridViewSale.Rows[i].Cells[1].Value + "',N'" + dataGridViewSale.Rows[i].Cells[2].Value + "',N'" + dataGridViewSale.Rows[i].Cells[3].Value + "','" + dataGridViewSale.Rows[i].Cells[4].Value + "','" + dataGridViewSale.Rows[i].Cells[5].Value + "','" + dataGridViewSale.Rows[i].Cells[6].Value + "','" + dataGridViewSale.Rows[i].Cells[7].Value + "','" + dataGridViewSale.Rows[i].Cells[8].Value + "','" + dataGridViewSale.Rows[i].Cells[9].Value + "',N'" + dataGridViewSale.Rows[i].Cells[10].Value + "','" + dataGridViewSale.Rows[i].Cells[11].Value + "','" + dataGridViewSale.Rows[i].Cells[12].Value + "',N'" + dataGridViewSale.Rows[i].Cells[13].Value + "')"; SqlDataAdapter sda = new SqlDataAdapter(query, conn); //اس ویلیوسے پہلے لازمی لکھناہے جو اردومیں ہو اوپروالی لائن کو غورسے دیکھیں N sda.SelectCommand.ExecuteNonQuery(); conn.Close(); } conn.Open(); string query1 = "INSERT INTO SaleMaster(VoucherId,BillNo,SaleDate,Transporter,PartyName,PartyNameText,Details,CashParty,Received,Remaining,TotalBill)VALUES('" + txtVoucherNo.Text + "','" + txtGrandSale.Text + "','" + dateTimeSale.Text + "',N'" + cBoxTransporter.Text + "',N'" + cBoxPartyName.Text + "',N'" + txtPartyName.Text + "',N'" + txtSDetail.Text + "',N'" + cBoxCashParty.Text + "','" + txtReceived.Text + "','" + lblRemaining.Text + "','" + lblTotalAmount.Text + "')"; SqlDataAdapter sda1 = new SqlDataAdapter(query1, conn); sda1.SelectCommand.ExecuteNonQuery(); conn.Close(); MessageBox.Show("Saved"); for (int i = 0; i < dataGridViewSale.Rows.Count - 1; i++) { conn.Open(); string query = "INSERT INTO Vouchers(SaleId,BillNo,Date,PartyName,Debit,Details)VALUES('" + txtInvoiceId.Text + "','" + txtGrandSale.Text + "','" + dateTimeSale.Text + "',N'" + cBoxPartyName.Text + "','" + dataGridViewSale.Rows[i].Cells[9].Value + "',N'" + txtSDetail.Text + "')"; SqlDataAdapter sda = new SqlDataAdapter(query, conn); //اس ویلیوسے پہلے لازمی لکھناہے جو اردومیں ہو اوپروالی لائن کو غورسے دیکھیں N sda.SelectCommand.ExecuteNonQuery(); conn.Close(); //SqlCommand cmd = new SqlCommand("INSERT INTO Vouchers (SaleId,BillNo,Date,PartyName,Debit,Details) values (@SaleId,@BillNo,@Date,@PartyName,@Debit,@Details)", conn); //cmd.Parameters.AddWithValue("@SaleId", txtInvoiceId.Text); //cmd.Parameters.AddWithValue("@BillNo", txtGrandSale.Text); //cmd.Parameters.AddWithValue("@Date", dateTimeSale.Text); //cmd.Parameters.AddWithValue("@PartyName", cBoxPartyName.Text); //cmd.Parameters.AddWithValue("@Debit", dataGridViewSale.Rows[i].Cells[9].Value); //cmd.Parameters.AddWithValue("@Details", txtSDetail.Text); //conn.Open(); //cmd.ExecuteNonQuery(); //conn.Close(); //MessageBox.Show("Added successfully!"); } for (int i = 0; i < dataGridViewSale.Rows.Count - 1; i++) { conn.Open(); string query = "INSERT INTO Vouchers(SaleId,BillNo,Date,PartyName,Credit,Details)VALUES('" + txtInvoiceId.Text + "','" + txtGrandSale.Text + "','" + dateTimeSale.Text + "',N'" + dataGridViewSale.Rows[i].Cells[13].Value + "','" + dataGridViewSale.Rows[i].Cells[9].Value + "',N'" + txtSDetail.Text + "')"; SqlDataAdapter sda = new SqlDataAdapter(query, conn); //اس ویلیوسے پہلے لازمی لکھناہے جو اردومیں ہو اوپروالی لائن کو غورسے دیکھیں N sda.SelectCommand.ExecuteNonQuery(); conn.Close(); } conn.Open(); string query3 = "INSERT INTO Vouchers(SaleId,BillNo,Date,PartyName,Debit,Details)VALUES('" + txtInvoiceId.Text + "','" + txtGrandSale.Text + "','" + dateTimeSale.Text + "',N'" + cBoxCashParty.Text + "','" + txtReceived.Text + "',N'" + txtSDetail.Text + "')"; SqlDataAdapter sda3 = new SqlDataAdapter(query3, conn); //اس ویلیوسے پہلے لازمی لکھناہے جو اردومیں ہو اوپروالی لائن کو غورسے دیکھیں N sda3.SelectCommand.ExecuteNonQuery(); conn.Close(); conn.Open(); string query2 = "INSERT INTO Vouchers(SaleId,BillNo,Date,PartyName,Credit,Details)VALUES('" + txtInvoiceId.Text + "','" + txtGrandSale.Text + "','" + dateTimeSale.Text + "',N'" + cBoxPartyName.Text + "','" + txtReceived.Text + "',N'" + txtSDetail.Text + "')"; SqlDataAdapter sda2 = new SqlDataAdapter(query2, conn); //اس ویلیوسے پہلے لازمی لکھناہے جو اردومیں ہو اوپروالی لائن کو غورسے دیکھیں N sda2.SelectCommand.ExecuteNonQuery(); conn.Close(); this.dataGridViewSale.DataSource = null; this.dataGridViewSale.Rows.Clear(); } else { MessageBox.Show("No Bill Amount"); } } //try //{ //} //catch //{ //} private void txtReceived_TextChanged(object sender, EventArgs e) { try { lblRemaining.Text = (int.Parse(lblTotalAmount.Text) - int.Parse(txtReceived.Text)).ToString(); } catch { } } private void txtPacking_TextChanged(object sender, EventArgs e) { try { txtTotalWeight.Text = (float.Parse(txtPacking.Text) * float.Parse(txtQty.Text)).ToString(); } catch { } } private void btnNew_Click(object sender, EventArgs e) { txtSDetail.Clear(); this.Close(); FormSale sale = new FormSale(); //if ((sale.lblTotalAmount.Text)=>0) //{ //} sale.Show(); } private void btnEdit_Click(object sender, EventArgs e) { FormSaleEdit edit = new FormSaleEdit(); edit.Show(); //FormSaleEdit saleEdit = new FormSaleEdit(); //saleEdit.Show(); } private void txtGrandSale_TextChanged(object sender, EventArgs e) { } public void SaleNo(string saleNumber) { saleNumber = txtGrandSale.Text; } //private void btnPrevious_Click(object sender, EventArgs e) //{ // SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); // for (int i = 0; i < dataGridViewSale.Rows.Count - 1; i--) // { // conn.Open(); // string query = "SELECT*FROM SaleItem where BillNO='" + txtGrandSale.Text + "' "; // sda = new SqlDataAdapter(query, conn); // dt = new DataTable(); // sda.Fill(dt); // dgvSale.DataSource = dt; // conn.Close(); // } //} void fillcBoxTransporter() { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); sda = new SqlDataAdapter("select * from Transporter", conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxTransporter.DataSource = dt; cBoxTransporter.DisplayMember = "Name"; conn.Close(); } void fillcBoxPartyName() { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); sda = new SqlDataAdapter("select PartyName from Parties", conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxPartyName.DataSource = dt; cBoxPartyName.DisplayMember = "PartyName"; conn.Close(); } void fillcBoxICode() { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); sda = new SqlDataAdapter("select * from Items", conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxICode.DataSource = dt; cBoxICode.DisplayMember = "ItemCode"; conn.Close(); } void fillcBoxLocation() { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); sda = new SqlDataAdapter("select * from Locations", conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxLocation.DataSource = dt; cBoxLocation.DisplayMember = "Location"; conn.Close(); } void fillcBoxItemParty() { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); sda = new SqlDataAdapter("select * from Parties", conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxItemParty.DataSource = dt; cBoxItemParty.DisplayMember = "PartyName"; conn.Close(); } void fillcBoxCashParty() { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); sda = new SqlDataAdapter("select * from Parties where Id=2", conn); DataTable dt = new DataTable(); sda.Fill(dt); cBoxCashParty.DataSource = dt; cBoxCashParty.DisplayMember = "PartyName"; conn.Close(); } public void autoFillFields() { //conn = new SqlConnection(); //string query = "select *from Items where ItemCode='"+cBoxICode.Text+"' "; //cmd = new SqlCommand(query, conn); //SqlDataReader sdr; //try //{ // conn.Open(); // sdr = cmd.ExecuteReader(); // while (sdr.Read()) // { // string iCode = sdr.GetInt32(1).ToString(); // string iParty = sdr.GetString(2); // string iName = sdr.GetString(3); // string iPacking = sdr.GetInt32(4).ToString(); // string iUnit = sdr.GetInt32(5).ToString(); // cBoxICode.Text = iCode; // cBoxItemParty.Text = iParty; // txtItemName.Text = iName; // txtPacking.Text = iPacking; // txtUnit.Text = iUnit; // } // conn.Close(); //} //catch //{ //} // conn.Open(); //string query = "select *from Items where ItemCode='" + cBoxICode.SelectedItem.ToString() + "' "; //sda = new SqlDataAdapter(query, conn); //sda.SelectCommand.ExecuteNonQuery(); // dt = new DataTable(); // sda = new SqlDataAdapter(cmd); // sda.Fill(dt); // foreach (DataRow dr in dt.Rows) // { // cBoxICode.Text = dr["ItemCode"].ToString(); // txtItemName.Text = dr["ItemName"].ToString(); // txtPacking.Text = dr["Packing"].ToString(); // txtUnit.Text = dr["Unit"].ToString(); // cBoxItemParty.Text = dr["ItemParty"].ToString(); // } // conn.Close(); } private void cBoxICode_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); conn.Open(); cmd = new SqlCommand("SELECT *FROM Items where ItemCode='" + cBoxICode.Text + "' ", conn); cmd.ExecuteNonQuery(); SqlDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { string iName = (string)dr["ItemName"].ToString(); txtItemName.Text = iName; string iPacking = (string)dr["Packing"].ToString(); txtPacking.Text = iPacking; string iUnit = (string)dr["Unit"].ToString(); txtUnit.Text = iUnit; string iParty = (string)dr["ItemParty"].ToString(); cBoxItemParty.Text = iParty; } conn.Close(); } private void cBoxICode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { if (string.IsNullOrEmpty(cBoxICode.Text)) { ((System.Windows.Forms.ComboBox)sender).DroppedDown = true; } else { cBoxLocation.Focus(); } } //else if (e.KeyCode==Keys.F1) //{ // cBoxICode.Focus(); // ((System.Windows.Forms.ComboBox)sender).DroppedDown = true; //} else if (e.KeyCode==Keys.Insert) { txtReceived.Focus(); } } private void cBoxTransporter_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { cBoxPartyName.Focus(); } else if (e.KeyCode == Keys.F1) { cBoxTransporter.Focus(); ((System.Windows.Forms.ComboBox)sender).DroppedDown = true; } } private void cBoxPartyName_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtPartyName.Focus(); } else if (e.KeyCode == Keys.F1) { cBoxPartyName.Focus(); ((System.Windows.Forms.ComboBox)sender).DroppedDown = true; } txtPartyName.Text = cBoxPartyName.Text; } private void txtSDetail_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { cBoxICode.Focus(); } } private void cBoxLocation_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtQty.Focus(); } } private void txtQty_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtTotalWeight.Focus(); } } private void txtTotalWeight_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtRate.Focus(); } } private void txtRate_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtMarka.Focus(); } } private void txtMarka_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { txtBagsRate.Focus(); } } private void btnSave_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnPrint.Focus(); } } private void btninsert_KeyDown_1(object sender, KeyEventArgs e) { //if (e.KeyCode==Keys.Tab) //{ // cBoxICode.Focus(); //} } private void txtBagsRate_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { DataTable dt = new DataTable(); dt.Columns.Add("LineId"); dt.Columns.Add("code"); dt.Columns.Add("item"); dt.Columns.Add("location"); dt.Columns.Add("qty"); dt.Columns.Add("packing"); dt.Columns.Add("totalWeight"); dt.Columns.Add("unit"); dt.Columns.Add("rate"); dt.Columns.Add("amount"); dt.Columns.Add("marka"); dt.Columns.Add("bagsRate"); dt.Columns.Add("bagsAmount"); dt.Columns.Add("itemParty"); DataRow row = dt.NewRow(); row["LineId"] = cBoxICode.Text; row["code"] = cBoxICode.Text; row["item"] = txtItemName.Text; row["location"] = cBoxLocation.Text; row["qty"] = txtQty.Text; row["packing"] = txtPacking.Text; row["totalWeight"] = txtTotalWeight.Text; row["unit"] = txtUnit.Text; row["rate"] = txtRate.Text; row["amount"] = txtTotalAmount.Text; row["marka"] = txtMarka.Text; row["bagsRate"] = txtBagsRate.Text; row["bagsAmount"] =txtBagsAmount.Text; row["itemParty"] = cBoxItemParty.Text; dt.Rows.Add(row); foreach (DataRow Drow in dt.Rows) { int num = dataGridViewSale.Rows.Add(); dataGridViewSale.Rows[num].Cells[0].Value = Drow["LineId"].ToString(); dataGridViewSale.Rows[num].Cells[1].Value = Drow["code"].ToString(); dataGridViewSale.Rows[num].Cells[2].Value = Drow["item"].ToString(); dataGridViewSale.Rows[num].Cells[3].Value = Drow["location"].ToString(); dataGridViewSale.Rows[num].Cells[4].Value = Drow["qty"].ToString(); dataGridViewSale.Rows[num].Cells[5].Value = Drow["packing"].ToString(); dataGridViewSale.Rows[num].Cells[6].Value = Drow["totalWeight"].ToString(); dataGridViewSale.Rows[num].Cells[7].Value = Drow["unit"].ToString(); dataGridViewSale.Rows[num].Cells[8].Value = Drow["rate"].ToString(); dataGridViewSale.Rows[num].Cells[9].Value = Drow["amount"].ToString(); dataGridViewSale.Rows[num].Cells[10].Value = Drow["marka"].ToString(); dataGridViewSale.Rows[num].Cells[11].Value = Drow["bagsRate"].ToString(); dataGridViewSale.Rows[num].Cells[12].Value = Drow["bagsAmount"].ToString(); dataGridViewSale.Rows[num].Cells[13].Value = Drow["itemParty"].ToString(); //cBoxTransporter.Text = ""; //cBoxPartyName.Text = ""; cBoxICode.Text=""; txtItemName.Clear(); txtQty.Clear(); txtTotalWeight.Clear(); txtPacking.Clear(); txtRate.Clear(); txtTotalAmount.Clear(); //txtReceived.Clear(); try { int A = 0, B = 0; for (A = 0; A < dataGridViewSale.Rows.Count; ++A) { B += Convert.ToInt32(dataGridViewSale.Rows[A].Cells[4].Value); } lblTotalTadad.Text = B.ToString(); int C = 0, D = 0; for (C = 0; C < dataGridViewSale.Rows.Count; ++C) { D += Convert.ToInt32(dataGridViewSale.Rows[C].Cells[12].Value); } lblTotalBardana.Text = D.ToString(); } catch { } } try { int E = 0, F = 0; for (E = 0; E < dataGridViewSale.Rows.Count; ++E) { F += Convert.ToInt32(dataGridViewSale.Rows[E].Cells[9].Value); } lblTotalAmount.Text = F.ToString(); } catch { } cBoxICode.Focus(); } } private void btnPrint_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnNew.Focus(); } } private void btnNew_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { cBoxTransporter.Focus(); } } private void txtReceived_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { btnSave.Focus(); } } private void txtSDetail_KeyDown_1(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { cBoxICode.Focus(); } } private void txtPartyName_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.Enter) { txtSDetail.Focus(); } } private void btnNext_Click(object sender, EventArgs e) { } private void btnUpdate_Click(object sender, EventArgs e) { //SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True"); //conn.Open(); //string query = "Update Id, VoucherId, BillNo, SaleDate, Transporter,PartyId,PartyName,PartyNameText,Details,CashParty,Received,Remaining,TotalBill, LineId, ItemCode, ItemName, Location, Qty,Packing, TotalWeight, Unit,Rate, Amount, Lot, BagsRate, BagsAmountEstimate,ItemParty,ItemId, LocationId FROM SaleBillView where BillNO='" + txtGrandSale.Text + "' "; //sda = new SqlDataAdapter(query, conn); //dt = new DataTable(); //sda.Fill(dt); //dgvSale.DataSource = dt; //conn.Close(); //dbo.SaleMaster.Id, dbo.SaleMaster.VoucherId, dbo.SaleMaster.BillNo, dbo.SaleMaster.SaleDate, dbo.SaleMaster.Transporter, dbo.SaleMaster.PartyId, dbo.SaleMaster.PartyName, // dbo.SaleMaster.PartyNameText, dbo.SaleMaster.Details, dbo.SaleMaster.CashParty, dbo.SaleMaster.Received, dbo.SaleMaster.Remaining, dbo.SaleMaster.TotalBill, dbo.SaleItem.LineId, // dbo.SaleItem.ItemCode, dbo.SaleItem.ItemName, dbo.SaleItem.Location, dbo.SaleItem.Qty, dbo.SaleItem.Packing, dbo.SaleItem.TotalWeight, dbo.SaleItem.Unit, dbo.SaleItem.Rate, dbo.SaleItem.Amount, // dbo.SaleItem.Lot, dbo.SaleItem.BagsRate, dbo.SaleItem.BagsAmountEstimate, dbo.SaleItem.ItemParty, dbo.SaleItem.ItemId, dbo.SaleItem.LocationId } }}What I have tried:the codes above i have tried by different ways.推荐答案Don’t do it like that!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。 连接字符串时会导致问题,因为SQL会收到如下命令: Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.When you concatenate strings, you cause problems because SQL receives commands like:SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令: The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'哪个SQL看作三个单独的命令: Which SQL sees as three separate commands:SELECT * FROM MyTable WHERE StreetAddress = 'x';完全有效的SELECT A perfectly valid SELECTDROP TABLE MyTable;完全有效的删除表格通讯和 A perfectly valid "delete the table" command--'其他一切都是评论。 所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。 所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?And everything else is a comment.So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you? 这篇关于如何将数据从一个C#windowsform更新为3个SQL服务器表,或者在保存之前恢复到saleinvoice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 08:31