none
How to pass database from one form to another form for iteration of database. RRS feed

  • Question

  • I have created connection for SQL server management studio on form1 and i want to pass server name and database name to another form form2. After passing of server name and database name on form to i want to iterate database so that i can perform some operation on those tables presented in database.

    //my code on form1 for to return server name and database name.

    public Server GetServer1()
            {
                return server1;
            }

            public string GetDatabase1()
            {
                return cboDatabase1.Text;
            }

     

    Ajit

    Wednesday, December 20, 2017 6:25 AM

Answers

  • Hi Risu Raj,

    The following document provide for approaches about Passing Data Between Forms.

    #The Constructor Approach

    Step 1

    Code a constructor for form2 class as below:

    public Form2(string servername, string databasename)
    {
      InitializeComponent(); 
      label1.Text=servername+databasename;
    }


    Step 2

    Instantiate form2 class in form1’s button click event handler as below:

    private void button1_Click(object sender, System.EventArgs e)
    {
    
    string serverName = "YourServerName";
    
    string databaseName = "YourDatabaseName";
    
    
        Form2 frm=new Form2(serverName, databaseName );
        frm.Show();
    }
    

    For more information, please refer to:https://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 20, 2017 8:22 AM
    Moderator

All replies

  • Hi Risu Raj,

    The following document provide for approaches about Passing Data Between Forms.

    #The Constructor Approach

    Step 1

    Code a constructor for form2 class as below:

    public Form2(string servername, string databasename)
    {
      InitializeComponent(); 
      label1.Text=servername+databasename;
    }


    Step 2

    Instantiate form2 class in form1’s button click event handler as below:

    private void button1_Click(object sender, System.EventArgs e)
    {
    
    string serverName = "YourServerName";
    
    string databaseName = "YourDatabaseName";
    
    
        Form2 frm=new Form2(serverName, databaseName );
        frm.Show();
    }
    

    For more information, please refer to:https://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 20, 2017 8:22 AM
    Moderator

  • #The Constructor Approach

    Step 1

    Code a constructor for form2 class as below:

    public Form2(string servername, string databasename)
    {
      InitializeComponent(); 
      label1.Text=servername+databasename;
    }


    Step 2

    Instantiate form2 class in form1’s button click event handler as below:

    private void button1_Click(object sender, System.EventArgs e)
    {
    
    string serverName = "YourServerName";
    
    string databaseName = "YourDatabaseName";
    
    
        Form2 frm=new Form2(serverName, databaseName );
        frm.Show();
    }


    I think by this way i will get only name of server and database. I want to iterate database also on form2 page for getting all table name present in that database. 

    Ajit

    Wednesday, December 20, 2017 10:07 AM
  • Hi Risu Raj,

    >>I think by this way i will get only name of server and database. I want to iterate database also on form2 page for getting all table name present in that database. 

    Based on your description, it seems that it is a another problem, which you have create a new thread, and I have provided the code to retrieve data table name from database.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 22, 2017 7:16 AM
    Moderator