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

            //Create a connection to the SQL Server; modify the connection string for your environment.

            SqlConnection MyConnection = new SqlConnection(“server=(local);database=pubs;UID=sa;PWD=;”);

            //Create a DataAdapter, and then provide the name of the stored procedure.

            SqlDataAdapter MyDataAdapter = new SqlDataAdapter(“GetAuthorsByLastName”, MyConnection);

            //Set the command type as StoredProcedure.

            MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

            //Create and add a parameter to Parameters collection for the stored procedure.

            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter(“@au_lname”, SqlDbType.VarChar, 40));

            //Assign the search value to the parameter.

            MyDataAdapter.SelectCommand.Parameters[“@au_lname”].Value = (TextBox1.Text).Trim();

            //Create and add an output parameter to the Parameters collection.

            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter(“@RowCount”, SqlDbType.Int, 4));

            //Set the direction for the parameter. This parameter returns the Rows that are returned.

            MyDataAdapter.SelectCommand.Parameters[“@RowCount”].Direction = ParameterDirection.Output;

            //Create a new DataSet to hold the records.

            DataSet DS = new DataSet();

           

            //Fill the DataSet with the rows that are returned.

            MyDataAdapter.Fill(DS, “AuthorsByLastName”);

            //Get the number of rows returned, and assign it to the Label control.

            //Label2.Text = DS.Tables(0).Rows.Count().ToString() & ” Rows Found!”

            Label2.Text = MyDataAdapter.SelectCommand.Parameters[1].Value + ” Rows Found!”;

            //Set the data source for the DataGrid as the DataSet that holds the rows.

            DataGrid1.DataSource = DS.Tables[“AuthorsByLastName”].DefaultView;

            //NOTE: If you do not call this method, the DataGrid is not displayed!

            DataGrid1 .DataBind();

            MyDataAdapter.Dispose(); //Dispose the DataAdapter.

            MyConnection.Close(); //Close the connection.

}