Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

executing a stored procedure with a button in a winapp c#?

Status
Not open for further replies.

hedub

Technical User
Mar 5, 2003
27
NO
I have several textfields, lets say forname, surename and telephone. With a click on a button I want to execute a stored procedure. The procedure inserts the values from textfields into a table. How do I program the click event so the procedure will run/ be executed?
 
Here I modified code that is working with information that you provided e.g. removed try-catch and reference to your fileds:
//

private System.Windows.Forms.Button InsertButton;
this.InsertButton.Click += new System.EventHandler(this.InsertButton_Click);
//

// Handler to insert in SQL table 3 values edited in the 3 TextBox controls
// m_ConnectionString stored somewhere in your app or retrieved from the exe.config
private void InsertButton_Click(object sender, System.EventArgs e)
{
int vSQLReturn = Insert(m_ConnectionString, textBox1.Text.ToString().Trim(),textBox2.Text.ToString().Trim(),textBox3.Text.ToString().Trim());
if (vSQLReturn == 1)
{
//insert successfully
}
else
{
// insert error

}
}
// cOnnect to a SQL database and insert in a table 3 values passed as parameters
private int Insert(string sConnectionString, string sForeName, string sSureName, string sTelephone )
{
int vReturn=0;
SqlConnection SQLConnection = new SqConnection(sConnectionString);
SqlDataAdapter vSqlDataAdapter=new SqlDataAdapter();
SQLConnection.Open();
vSqlDataAdapter.SelectCommand = new SqlCommand("sp_Insert",SQLConnection);
vSqlDataAdapter.SelectCommand.CommandType =CommandType.StoredProcedure;
SqlParameter vSqlParameter= vSqlDataAdapter.SelectCommand.Parameters.Add("@ReturnValue", SqlDbType.Int);
vSqlParameter.Direction = ParameterDirection.ReturnValue;
vSqlParameter= vSqlDataAdapter.SelectCommand.Parameters.Add("@forname", sForeName );
vSqlParameter= vSqlDataAdapter.SelectCommand.Parameters.Add("@surename", sSureName);
vSqlParameter= vSqlDataAdapter.SelectCommand.Parameters.Add("@telephone", sTelephone);
DataSet vDataSet = new DataSet();
vSqlDataAdapter.Fill(vDataSet);
vReturn= (Int32) vSqlDataAdapter.SelectCommand.Parameters["@ReturnValue"].Value;
return(vReturn);
}

Stored Procedure which will insert the value passed as parameters
CREATE PROCEDURE dbo.sp_Insert
(
@forename varchar(50),
@surename varchar(50),
@telephone varchar(50)
)
AS
SET NOCOUNT ON
Set @TableName='_tWork'
Set @Cmd='INSERT INTO ' + @TableName +....
EXEC(@Cmd)
RETURN(1)
GO
//
You also can use ExecuteNonQuery() to execute the stored procedure but in my example I assumed many return values from the stored procedure such as records and these will be stored in the vDataSet object.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top