save data from two forms into same table in winforms application RRS feed

  • Question

  • I have two winforms where form 1 is used to enter data from first seven fields and other forms is used to enter for last three fields , where i have the table stored in MSSQL server , but the problem is whenever i tried to save data in second forms it is getting stored in first row itself instead of the row where the first form is updating , so anyone please help how to link these two forms and get them saved in the same row :

    below is the code for saving the data in Second form, and images are attached:

    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
    using System.Data.SqlClient;  
    namespace Windows  
        public partial class Form2 : Form  
            public Form1()  
            private void btnInsert_Click(object sender, EventArgs e)  
                SqlConnection con = new SqlConnection(cs);  
                SqlCommand cmd;  
                string s="insert into Student values(@p1,@p2,@p3)";  
                cmd=new SqlCommand(s,con);  
                cmd.CommandType = CommandType.Text;  
                int i=cmd.ExecuteNonQuery();  
                MessageBox.Show(i+ " Row(s) Inserted ");  

    samedi 12 décembre 2020 05:11

Toutes les réponses

  • You need to learn how to use Object Oriented Programming. You need to learn how to use a custom class, a data container. The data container matches the fields on the two forms you are wanting to save to the database.

    Properties - C# Programming Guide | Microsoft Docs

    Auto-Implemented Properties - C# Programming Guide | Microsoft Docs

    There should be no actual saving of data in the Insert button click. You need to move the code to a custom method you make and the button-event calls the method to save the data, which is a basic form of SoC.

     Separation of concerns - Wikipedia

    So you create the custom object in form1 and populate it. You pass the custom object to form2 and populate it in form2. You give the custom object back to form1 from form2. From2 does no saving.  Form1 does the actual save using the custom object that was populated by form1 and form2.

    That's the basics of what you need to do.

    • Modifié DA924x dimanche 13 décembre 2020 08:55
    dimanche 13 décembre 2020 08:50
  • Hi

    public class Form1 : Form


        public Form1() { }


        private void btnGetUserInput_Click(object sender, EventArgs e)


            Form2 form2 = new Form2();





    public class Form2 : Form


        public Form2() { }


        private void btnSaveInput_Click(object sender, EventArgs e)


            Form1 form1 = new Form1();

            form1.???  // How do I show my values on the first form?




    Best Regards.

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

    dimanche 13 décembre 2020 09:38