0

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

2 Answers2

1

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();
    }
}
T.S.
  • 14,772
  • 10
  • 47
  • 66
  • The type or namespace name 'Iview1' could not be found (are you missing a using directive or an assembly reference?) @T.S how can I reference it , to be useful – Leonard Gashi Jun 09 '20 at 10:39
  • @LeonardGashi `IView1` and `IView2` should be interfaces you need to declare. These interfaces should contain properties that you save in your DB. This is MVP or Model View Presenter pattern. See here https://stackoverflow.com/questions/2056/what-are-mvp-and-mvc-and-what-is-the-difference Your form becomes view model and you can easily take the values you need as they will be mapped on the form to the control values https://www.c-sharpcorner.com/UploadFile/vendettamit/simplest-example-of-mvp-design-patter-of-Asp-Net/ – T.S. Jun 09 '20 at 14:04
0

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.

  • **#2** will introduce need for state control. What if form was closed and restarted and the saved object still has old form data? **#1** will introduce need for data integrity control. What if you have not collected 1 or both forms data yet and pressed "Save"? – T.S. Jun 08 '20 at 21:09
  • You can make this however complicated you like. Clicking ‘Save’ without having the data is a view-level problem. The question imo. is all about controlling state. The key take away is to decouple the data, its view and the processing logic – Norbert Hüthmayr Jun 09 '20 at 00:05
  • @NorbertHüthmayr any short example can be helpful, cause I am new to programming – Leonard Gashi Jun 09 '20 at 07:35
  • @LeonardGashi A whole example would be too much but I found an interesting link that might help you understand the concept. https://stackoverflow.com/a/122565/13567181 – Norbert Hüthmayr Jun 09 '20 at 13:05