Leonard Gashi
Leonard Gashi

Reputation: 1

Insert data into SQL table from two forms C# winform

I am trying to insert data from frm1 and frm2 into SQL Server at the same time. frm1 contains product information (barcode, qty , price, vat, total etc) and frm2 (contains payments information like cash and change). The idea is that when the user clicks the btnfrm2 the data from frm1 should be passed into frm2 (but not displayed), and in frm2 the user gives the payment info (cash and change) and after clicking btnsave the data from frm1 and frm2 should be inserted into the database.

I created a class, and a method to pass data to frm2.

    internal void mbushe(string[] args)

    {

        for (int i = 0; i < dataTable.Rows.Count; i++)

        {

            arka_data ad = new arka_data();

            ad.NR = int.Parse(txtnrfatures.Text);

            ad.VLERATVSHTOTAL = float.Parse(textBox1.Text);

            ad.BARKODI = int.Parse(dataTable.Rows[i][0].ToString());

            ad.EMERTIMI = dataTable.Rows[i][1].ToString();

            ad.SASIA = int.Parse(dataTable.Rows[i][2].ToString());

            ad.CMIMI = int.Parse(dataTable.Rows[i][3].ToString());

            ad.TVSH = int.Parse(dataTable.Rows[i][4].ToString());

            ad.NENTOTALI = float.Parse(txttotali.Text);

            ad.ZBRITJA = float.Parse(txtzbritja.Text);

            ad.TOTALI = float.Parse(totali.Text);

            ad.KOHA = DateTime.Now;

            ad.KASIERI = lbluser.Text;

            ad.KLIENTI = cmbklienti.Text;

            ad.VLERAETVSH = float.Parse(dataTable.Rows[i][7].ToString());

            ad.VLERAPATVSH = float.Parse(dataTable.Rows[i][6].ToString());

            ad.NRATIKUJVE = int.Parse(lblnumri.Text);

            ad.TOTALIPCS = float.Parse(dataTable.Rows[i][5].ToString());



        }

    }



    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; }

    }

and in the second form I will use the elements of method( from first form)

         cmd.Parameters.Add(new SqlParameter("@nrfatures", mbushe.NR);

         cmd.Parameters.Add(new SqlParameter("@klienti", mbushe.Barkodi)); etc

while mbushe is the method from first form

Upvotes: 0

Views: 204

Answers (2)

Norbert H&#252;thmayr
Norbert H&#252;thmayr

Reputation: 515

I would recommend that you build an object from the data on the UI and then either

  1. Pass it along (from form to form) while filling it with more and more information or
  2. Store it in some kind of repository so every part of the code-base has access to (preferred)

When the user then clicks the btnsave button on Form2, you simply call a code that persists that object.

This way you are keeping the form, its data, and the database interaction logic separate from each other.

Upvotes: 0

T.S.
T.S.

Reputation: 19330

Best if your forms use MVP pattern, AKA inherit from interface. Example

public class MyForm1 : Form, IView1
{
    public string SomeData { get { return MyControl1.Text } }
    . . . . 
}

public class MyForm2 : Form, IView2
{
    public string SomeOtherData { get { return MyControl1.Text } }
    . . . . 
}

Then you create persister, which collects the data from both forms and saves

public class FormDataPersister
{

    private IView1 _v1;
    private IView2 _v2;

    public class FormDataPersister(IView1 form1Data, IView2 form2Data)
    {
        _v1 = form1Data;
        _v2 = form2Data;        
    }

    public void Save()
    {

        // HERE collect your data into parameters and SAVE
        // EXAMPLE
         . .  . . . 
        cmd.Parameters.Add(new SqlParameter("@klienti", _v1.Barkodi));
        cmd.ExecuteNonQuery();
    }
}

Upvotes: 1

Related Questions