locked
Insert data from two forms into sql c# RRS feed

  • Question

  • User-1625756790 posted

    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:

    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

    Sunday, May 24, 2020 4:17 PM

All replies

  • User288213138 posted

    Hi Immeo,

    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:

    According to your description, I couldn’t understand your requirement clearly.

    Do you want to insert data from another webform page into the database through an event in the webform page?

    If this is the case, you need to pass all the data from another webform and then insert it.

    About how to pass data between two webforms, you can try to use session.

    More information about how to pass data you can refer to this link: https://stackoverflow.com/a/26614744/13336642

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    Monday, May 25, 2020 3:37 AM
  • User-2054057000 posted

    There can be many ways to Insert data from two forms into sql c# I give you 2 ways:

    1. First you can use 2 hidden fields - one each inside each the form. On the click of the button (you do this with javascript) get the value of data from other control and put it inside this hidden control then on form submission the hidden field data will be posted to the server. This way you get the value of other data.  
    2. Second way is you use AJAX to call your server side code and send all your required data with it.  You can simply use jQuery AJAX method in this case. 

    Now to insert this data in your database you can simply call your stored procedure and send the values to be inserted to it's parameters. 

    Hope I am clear.

    Tuesday, June 2, 2020 12:50 PM