?
Solved

Passing variables from Form1 to Form2 and executing Store Procedure

Posted on 2005-04-17
1
Medium Priority
?
455 Views
Last Modified: 2010-04-16
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!
0
Comment
Question by:mhertzDEV
1 Comment
 
LVL 14

Accepted Solution

by:
existenz2 earned 2000 total points
ID: 13801803
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question