Click here to Skip to main content
14,698,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a small pos application where user can make payments and save db. Until now I stored data from one form(datagridview, textboxes etc), but now I decided to add one more form (for payments and the change). The idea is that the user call data from db in datagridview(***barcode, qty, name, price, total, vat et***c) , then press the btnpayment(***opens the 2nd form***), then the user give the the required data(give payment), then after clicking the pay button the data from two forms should be inserted into sql table

Now I want to save data from two forms into sql db in same time using same stored procedures.

The 2nd form has some textboxes (payments and change). Now upper code I want to put into 2nd form insert button , but don't know how to link two forms together. My question is what should I change in upper code to be able to put in 2nd form insert button, then insert 2 forms in same time 

What I have tried:




    foreach (DataGridViewRow row in dtgartikuj.Rows)


        if (!row.IsNewRow)


            SqlCommand cmd = new SqlCommand("insertfaturimi", conn);

            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.Add(new SqlParameter("@nrfatures", int.Parse(txtnrfatures.Text)));

            cmd.Parameters.Add(new SqlParameter("@klienti", cmbklienti.Text));

            cmd.Parameters.Add(new SqlParameter("@pagesa", faturimi));

            cmd.Parameters.Add(new SqlParameter("@nentotali", txttotali.Text));

            cmd.Parameters.Add(new SqlParameter("@zbritje", txtzbritja.Text));

            cmd.Parameters.Add(new SqlParameter("@totali", totali.Text));

            cmd.Parameters.Add(new SqlParameter("@vleratvsh", textBox1.Text));

            cmd.Parameters.Add(new SqlParameter("@nrartikujve", lblnumri.Text));

            cmd.Parameters.Add(new SqlParameter("@kasieri", lbluser.Text));

            cmd.Parameters.Add(new SqlParameter("@koha", DateTime.Now));

            cmd.Parameters.Add(new SqlParameter("@barkodi", row.Cells[0].Value));

            cmd.Parameters.Add(new SqlParameter("@emertimi", row.Cells[1].Value));

            cmd.Parameters.Add(new SqlParameter("@sasia", row.Cells[2].Value));

            cmd.Parameters.Add(new SqlParameter("@tvsh", row.Cells[4].Value));

            cmd.Parameters.Add(new SqlParameter("@cmimi", row.Cells[3].Value));

            cmd.Parameters.Add(new SqlParameter("@totalipcs", row.Cells[5].Value));

            cmd.Parameters.Add(new SqlParameter("@vlerapatvshpcs", row.Cells[6].Value));

            cmd.Parameters.Add(new SqlParameter("@vleraetvshpcs", row.Cells[7].Value));





catch (Exception ex)


    MessageBox.Show("Faturimi deshtoi" + ex.ToString());





Updated 20-May-20 9:43am

1 solution

Ideally, the second form shouldn't ever have to know anything about the details in the first form (it only should know about the total fee). Instead, the second form should validate the payment details. The first form can then use the return result of the second form to actually proceed; either by savinf the data to the database, if the second form returned a success, or by not saving and issuing a failure message, if the second form returned a failure.

using (Form2 form = new Form2(total))
   if (form.ShowDialog(this) == DialogResult.OK)
      // Save to database and issue success message
      // Issue a failure message

private decimal Total;

public Form2()

public Form2(decimal total) : this()
   Total = total;

private void buttonPlay_Click(object sender, EventArgs e)
   bool paymentIsValid = ProcessPayment();
   DialogResult = paymentIsValid ? DialogResult.OK : DialogResult.Cancel;

private bool ProcessPayment()
   // Handle the payment here.
   // Return true on success.
   // Return false on failure.
[no name] 26-May-20 6:48am
what to put inside of #processpayment method
phil.o 26-May-20 6:53am
Anything which aims at verifying that the payment has been actually done. This depends on so many factors that there is not a single code fitting all situations. See with the entity processing the payments to know how to properly use their services.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900