简体   繁体   中英

How to transfer data from one form to another in winforms c#?

  1. I want to create at 5-6 forms in windows application to insert data.
  2. Each form contain at-least 15-20 Controls. All Forms Belongs to different table. But some with same.
  3. I have to created stored "Next"named button on each form, such that when i click on next button all the information filled on that button get store some where
    and in this way after storing the information on the last button where a Submit button is dragged, on clicking on that submit button all the data got saved into the data base.
  4. Please tell me how can i store the data inserted on previous forms and call it on click event of submit button.

For now i am having all the controls on the same page and i have used these codes for insertion.

 private void submit_addbtn_Click(object sender, EventArgs e)
        {
            try
            {
                //personal data insert
                Personal per = new Personal();
                per.Name = nametxt.Text;
                per.FatherName = f_nametxt.Text;
                per.MotherName = m_nametxt.Text;
                per.Gotra = gotra_txt.Text;
                per.Panth = panthcb.Text;
                per.FamilyHead = fhntext.Text;
                per.Educationlvl = edulvlcb.Text;
                per.Education = educb.Text;
                per.Blood = bloodcb.Text;
                per.Gender = genderlist.Text;
                per.Marrital = MarritalStatus;
                per.DateOfBirth = dobdtp.Text;
                if (new InsertAction().Insertpersonal(per))
                {
                    MessageBox.Show("Personal Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Personal Insertion does not Happen ");
                }

                // spouse data insert
                Spouse sps = new Spouse();
                sps.Spousename = s_nametxt.Text;
                sps.Spouseeducationlvl = s_edulvlcb.Text;
                sps.Spouseeducation = s_educb.Text;
                sps.Spouseblood = s_bgcb.Text;
                sps.Spousedob = s_dobdtp.Text;
                if (new InsertAction().Insertspouse(sps))
                {
                    MessageBox.Show(" Spouse Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Spouse Insertion does not Happen ");
                }

                // Resident data insert
                Ressident resi = new Ressident();
                resi.RessiHnumber = ressi_numtxt.Text;
                resi.RessihCmplx = ressi_complextxt.Text;
                resi.RessiStrt = ressi_streettxt.Text;
                resi.RessiLandmrk = ressi_landtxt.Text;
                resi.RessiArea = ressi_areatxt.Text;
                resi.RessiCity = ressi_citytxt.Text;
                resi.RessiPhone = Convert.ToInt64(ressi_phnotxt.Text);
                resi.RessiMobile = Convert.ToInt64(mobi_notxt.Text);
                if (new InsertAction().Insertressident(resi))
                {
                    MessageBox.Show(" Ressident Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Ressident Insertion does not Happen ");
                }
                //occupation data insert
                Occupation ocp = new Occupation();
                ocp.Occuptype = occup_typetxt.Text;
                ocp.Occupadd = office_addresstxt.Text;
                ocp.Occupnature = occup_naturecb.Text;
                ocp.Occupphone = Convert.ToInt64(office_phno1txt.Text);
                ocp.Occupmobile = Convert.ToInt64(office_mobnotxt.Text);
                if (new InsertAction().Insertoccupation(ocp))
                {
                    MessageBox.Show(" Occupation Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Occupation Insertion does not Happen ");
                }


            }

Please Help me. Thank you.

For sending values between two forms, you may

1-> Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form as :

 Form1 obj = new Form1(Object);

2-> You may take a reference in to your first form in the second form.

In second form,

public Form1 objForm1;

and in First Form,

Form2 objForm2=new Form2();
Form2.objForm1=this;

and then you can use Form2's objForm1 to refer to Form1's textbox or any control.

EDIT :

Consider you want to send all values from Form1 to Form2

In your second form you must have a variable of type Form1 that refers to the prev form. So in second form,

public Form1 objForm1;

and then you need to send the current instance of the Form1 to Form2 as

Form2 objForm2=new Form2();
Form2.objForm1=this;

ie the objForm1 that you created in Form2 refers to this instance of Form1.

Now in Form2 you may use any of Form1's control or variable as,

Form1.TextBox1 or Form1.Variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM