Inserting data from two forms into sql table

muleo

Member
Joined
May 24, 2020
Messages
7
Programming Experience
1-3
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.

Before adding the 2nd form I used this code to insert data:

C#:
try
{
    conn.Open();

    foreach (DataGridViewRow row in dtgartikuj.Rows)
    {
        if (!row.IsNewRow)
        {
            SqlCommand cmd = new SqlCommand("insertfaturimi", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Clear();
            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));
            cmd.ExecuteNonQuery();
        }
    }
}

catch (Exception ex)
{
    MessageBox.Show("Faturimi deshtoi" + ex.ToString());
}

finally
{
    conn.Close();
}


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
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,937
Location
Chesapeake, VA
Programming Experience
10+
Unfortunately, since the names of your stored procedure parameters are in Albanian (?) it is hard for us to distinguish what the parameters are that you are passing in, and what you might need in your second form.

It may help us if you showed us what was in your stored procedure like when you asked a related question to this in CodeProject.

In general though the appropriate approach is this: You pass a data transfer object to both form 1 and form 2. Each form fills in the parts they know about in the DTO. Once you have all the data filled into the DTO, then you perform your SQL call passing in the gathered information.
 
Top Bottom