Passing variables from Form1 to Form2 and executing Store Procedure

Hola:

I'm trying to pass information from Form1 to Form2, i've seen several post with this information but none have worked in my case.

My Form1 has 2 textbox "FirstName" and "LastName" and a "Search" button.
My Form2 would display the results of the matching FirstName and LastName.  The Form2 is attached to a StoreProcedure that is expecting this 2 parameters.

How can i get the FirstName and LastName text from Form1 to Form2 in a way that i can execute the StoreProcedure with those parameters?


// MY FORM1 C#.NET 2003 CODE
namespace SearchApplication
{
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.TextBox txtFirstName;
            private System.Windows.Forms.TextBox txtLastName;
            private System.Windows.Forms.Button btnManual;
            private System.ComponentModel.Container components = null;

            .........

            private void btnManual_Click(object sender, System.EventArgs e)
            {

                  Form2 myForm2 = new Form2();
                  myForm2.Show();
            }



      }
}
      
// MY FORM2 C#.NET 2003 CODE
namespace SearchApplication
{
      public class Form2 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.DataGrid dataGrid1;
            private System.Data.SqlClient.SqlConnection sqlConnection1;
            private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
            private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
            private SalsaCasino.com_POS.DataSet1 dataSet12;

            ....
            
            #region Windows Form Designer generated code


            private void InitializeComponent()
            {
                  .....
                  // sqlSelectCommand1
                  this.sqlSelectCommand1.CommandText = "[scc_getCustomersFromSearch]";
                  this.sqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure;
                  this.sqlSelectCommand1.Connection = this.sqlConnection1;
                  this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
                  this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, <FIRSTNAME VARIABLE>));
                  this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, <LASTNAME VARIABLE>));
                  
                  ....
            }
            #endregion

            private void frmResults_Load(object sender, System.EventArgs e)
            {
                  dataSet12.Clear();
                  sqlDataAdapter1.Fill(dataSet12, "scc_getCustomersFromSearch");
                  
            }
      }
}
      
Thanks!
mhertzDEVAsked:
Who is Participating?
 
existenz2Commented:
Edit Form2 so it becomes like this:

public class Form2 : System.Windows.Forms.Form
{
private string firstName;
private string lastName;
...

public Form2(string firstName, string lastName) {
this.firstName = firstName;
this.lastName = lastName;

... (Rest of constructor)

InitializeComponent();

}

private void InitializeComponent()
          {
               .....
               // sqlSelectCommand1
               this.sqlSelectCommand1.CommandText = "[scc_getCustomersFromSearch]";
               this.sqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure;
               this.sqlSelectCommand1.Connection = this.sqlConnection1;
               this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
               this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, firstName));
               this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, lastName));
               
               ....
          }
...
}

You can call Form2 now from Form1 the following way:

namespace SearchApplication
{
     public class Form1 : System.Windows.Forms.Form
     {
          private System.Windows.Forms.TextBox txtFirstName;
          private System.Windows.Forms.TextBox txtLastName;
          private System.Windows.Forms.Button btnManual;
          private System.ComponentModel.Container components = null;

          .........

          private void btnManual_Click(object sender, System.EventArgs e)
          {

               Form2 myForm2 = new Form2(txtFirstName.Text,txtLastName.Text);
               myForm2.Show();
          }



     }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.