Click here to Skip to main content
15,895,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i pass this method from another form to other



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





  }

  public void mbushe()

{

     arka_data dta  = new arka_data();

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

    {

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

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

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

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

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

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

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

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

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

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

        dta.KOHA = DateTime.Now;

        dta.KASIERI = lbluser.Text;

        dta.KLIENTI = cmbklienti.Text;

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

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

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

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

    }


The idea it that this class handle the data from form one , and use it(insert it into sql) on form two . How should I pass the object in another form, or how should I refer it. Also another question is the class populated correctly (from : textbox, labels and datagridview ). The method mbushe collects data , and this data should be passed one frm2. But mu question is how to get this data on frm2

What I have tried:

I created the method on frm1 , but don't have any clue how to cont. on. I tried Arka. Mbushe but getting this error ' is a 'method', which is not valid in the given contextThe idea is that during the insertion of data on frm2 I use this data like this
cmd.Parameters.Add(new SqlParameter("@nrfatures", mbushe.NR);
Posted
Updated 26-May-20 6:19am
v4

Don't. Form2 shouldn't know that Form1 exists, much less what methods it contains. Instead, it should use events to let Form1 know that something needs doing.

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
There are several ways, but one of the easiest is to create a Static class, see: https://www.dotnetperls.com/static[^]
Tutorial C# Static[^]

A quick and dirty way would be to use the static class Program which is already present in a Winforms project.
 
Share this answer
 
v3
Comments
[no name] 26-May-20 9:26am    
But I tried the static class,but don't work, it throwns this error code"Error 4 Cannot create an instance of the static class "
RickZeeland 26-May-20 9:30am    
That's right, you can't create instances, just use the static class directly.
Take a good look at the Dotnetperls examples :)
[no name] 26-May-20 9:36am    
But I am not trying to pass a empty class, I am trying to call a method which handles non-static data from textbox, datagridview.
RickZeeland 26-May-20 9:57am    
You are thinking too Object Oriented, think of the static class as a bag where both forms can put their data in and take it out too.
You have mixed several things!

1. Move arka_data into separate file
2. Find Program.cs file and add this line:
C#
public static List<arka_data> dta;

3. In Form1 constructor:
C#
public Form1()
{
    InitializeComponent();
    Program.dta = new List<arka_data>();
}

4. Somewhere after... fill in your dta list.
C#
public void mbushe()
{
     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());
        //and finally
        Program.dta.Add(ad);
    }

4. If you would like to edit single record in Form2, you have to create constructor which accepts arka_data class as a parameter. See solution #2 by OriginalGriff.
 
Share this answer
 
Comments
[no name] 26-May-20 12:23pm    
How should now I be able to store data into sql DB using these data. What code should I add to use the program.dta collected data. and instead of
txtnrfatures.Text
to use "program.dta.NR"

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));
Maciej Los 26-May-20 15:51pm    
program.dta.NR does not exist! Program.dta does exist!
Also arka_data ad = Program.dta.Where(x=> x.NR==some_value).SingleOrDefault() does exist!
So, you can use ad variable now.
[no name] 26-May-20 13:38pm    
How to refer in form2 to program.dta and use it?Because I am trying to create new instance of program.dta but not working
Also how to clear program.dta after earch insertation? Cause this program has to insert a lot of data
Maciej Los 26-May-20 15:47pm    
You don't need to create new instance of Program.dta! Imagine, your data are stored in variable.

Sorry, but i do not understand the rest of your question.
[no name] 26-May-20 15:52pm    
Okay how to address this variabile in 2nd form. So to be able to give from that variabile every parameter

cmd.Parameters.Add(new SqlParameter("@nrfatures", //variabile_parameter1);
cmd.Parameters.Add(new SqlParameter("@klienti", //variabile_parameter1);
cmd.Parameters.Add(new SqlParameter("@pagesa", //variabile_parameter1);

How should I address that variabile into 2nd form. Thanks again

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