【发布时间】:2020-05-24 16:46:43
【问题描述】:
我正在开发一个小型 pos 应用程序,用户可以在其中付款并将其保存到数据库中。到目前为止,我从一个表单(datagridview、文本框等)存储数据,但现在我决定再添加一个表单(用于处理付款)。想法是用户在 datagridview(*barcode, qty, name, price, total, vat et***c) 中调用 db 中的数据,然后按 btnpayment(***opens the 2nd form*),然后用户提供所需的数据(give payment),然后点击支付按钮后,两个表单的数据应该插入到sql表中 现在我想使用相同的存储过程将两个表单中的数据同时保存到 sql db 中。
在添加第二个表单之前,我使用此代码插入数据:
try
{
conn.Open();
foreach (DataGridViewRow row in dtgartikuj.Rows)
{
if (!row.IsNewRow)
{
SqlCommand commandinsert= new SqlCommand("insertfaturimi", conn);
commandinsert.CommandType = CommandType.StoredProcedure;
commandinsert.Parameters.Clear();
commandinsert.Parameters.Add(new SqlParameter("@nr", int.Parse(txtnr.Text)));
commandinsert.Parameters.Add(new SqlParameter("@client", cmbclient.Text));
commandinsert.Parameters.Add(new SqlParameter("@subtotal", txtsubtotal.Text));
commandinsert.Parameters.Add(new SqlParameter("@discount", txtdiscount.Text));
commandinsert.Parameters.Add(new SqlParameter("@total", txttotal.Text));
commandinsert.Parameters.Add(new SqlParameter("@vatvalue", txtvatvalue.Text));
commandinsert.Parameters.Add(new SqlParameter("@productnr", prodnr.Text));
commandinsert.Parameters.Add(new SqlParameter("@seller", lbluser.Text));
commandinsert.Parameters.Add(new SqlParameter("@time", DateTime.Now));
commandinsert.Parameters.Add(new SqlParameter("@barcode", row.Cells[0].Value));
commandinsert.Parameters.Add(new SqlParameter("@name", row.Cells[1].Value));
commandinsert.Parameters.Add(new SqlParameter("@qty", row.Cells[2].Value));
commandinsert.Parameters.Add(new SqlParameter("@vat", row.Cells[4].Value));
commandinsert.Parameters.Add(new SqlParameter("@price", row.Cells[3].Value));
commandinsert.Parameters.Add(new SqlParameter("@totalpcs", row.Cells[5].Value));
commandinsert.Parameters.Add(new SqlParameter("@vatvalueswithoutvatpcs", row.Cells[6].Value));
commandinsert.Parameters.Add(new SqlParameter("@vatvaluepcs", row.Cells[7].Value));
commandinsert.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Failed" + ex.ToString());
}
finally
{
conn.Close();
}
第二种形式有一些文本框(付款和找零)。现在我想将上面的代码放入第二个表单支付按钮,但不知道如何将两个表单链接在一起。我的问题是我应该在上面的代码中更改什么以便能够放入第二个表单插入按钮,然后同时插入两个表单(表单一个包含产品详细信息)和(第二个表单包含付款详细信息)
我添加了这个类代码
public class arka_data
{
public int NR { get; set; }
public int BARKODI { get; set; }
public string EMERTIMI { get; set; }
public int SASIA {get;set;}
public float CMIMI {get;set;}
public float TVSH { get; set; }
public float TOTAL { get; set; }
public float NENTOTALI { get; set; }
public float ZBRITJA { get; set; }
public float TOTALI { get; set; }
public DateTime KOHA { get; set; }
public string KASIERI { get; set; }
public string KLIENTI { get; set; }
public float VLERAETVSH { get; set; }
public float VLERAPATVSH { get; set; }
public int NRATIKUJVE { get; set; }
public float TOTALIPCS { get; set; }
public float VLERATVSHTOTAL { get; set; }
}
arka_data dta = new arka_data();
public void mbushe(string[] args)
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
dta.NR = int.Parse(txtnrfatures.Text);
dta.VLERATVSHTOTAL = float.Parse( textBox1.Text);
dta.BARKODI = int.Parse(dataTable.Rows[i][0].ToString());
dta.EMERTIMI = dataTable.Rows[i][1].ToString();
dta.SASIA = int.Parse(dataTable.Rows[i][2].ToString());
dta.CMIMI = int.Parse(dataTable.Rows[i][3].ToString());
dta.TVSH = int.Parse(dataTable.Rows[i][4].ToString());
dta.NENTOTALI = float.Parse(txttotali.Text);
dta.ZBRITJA = float.Parse(txtzbritja.Text);
dta.TOTALI = float.Parse(totali.Text);
dta.KOHA = DateTime.Now;
dta.KASIERI = lbluser.Text;
dta.KLIENTI = cmbklienti.Text;
dta.VLERAETVSH = float.Parse(dataTable.Rows[i][7].ToString());
dta.VLERAPATVSH = float.Parse(dataTable.Rows[i][6].ToString());
dta.NRATIKUJVE = int.Parse(lblnumri.Text);
dta.TOTALIPCS = float.Parse(dataTable.Rows[i][5].ToString());
但是不知道怎么调用form2上的方法
【问题讨论】:
-
您可以将数据添加到第二个表单中,在表单中,设置这些数据,然后在第二个表单关闭时,从第一个表单中引用数据并保存。