ข้ามไปที่เนื้อหาหลัก
Windows Dev Center
ลงชื่อเข้าใช้

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

  • คำถาม

  • 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()  
            {  
                InitializeComponent();  
            }  
              
            private void btnInsert_Click(object sender, EventArgs e)  
            {  
                SqlConnection con = new SqlConnection(cs);  
                SqlCommand cmd;  
                con.Open();  
                string s="insert into Student values(@p1,@p2,@p3)";  
                cmd=new SqlCommand(s,con);  
                cmd.Parameters.AddWithValue("@p1",rejectReason1ComboBox.Text);  
                cmd.Parameters.AddWithValue("@p2",rejectReason2ComboBox.Text);  
                cmd.Parameters.AddWithValue("@p3",rejectReason3ComboBox.Text);  
                cmd.CommandType = CommandType.Text;  
                int i=cmd.ExecuteNonQuery();  
                con.Close();  
                MessageBox.Show(i+ " Row(s) Inserted ");  
            }  
        }  
    }  

    12 ธันวาคม 2563 5:11

ตอบทั้งหมด

  • 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.

     
    • แก้ไขโดย DA924x 13 ธันวาคม 2563 8:55
    13 ธันวาคม 2563 8:50
  • Hi

    public class Form1 : Form

    {

        public Form1() { }

     

        private void btnGetUserInput_Click(object sender, EventArgs e)

        {

            Form2 form2 = new Form2();

            form2.ShowDialog();

        }

    }

     

    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?

            form1.ShowDialog();

        }

    }

    Best Regards.

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

    13 ธันวาคม 2563 9:38