Stored Procedure: OUT parameter
How to use OUT parameter in stored procedure? and how to use that OUT parameter in code behind?..................
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Subhendu DeRank #1,108 · 1.7K reputationPosted Dec 13, 2010, 3:34 AM EST
CREATE PROCEDURE prcTest
@pIn nvarchar(50),
@pOut nvarchar(50) OUT
AS
BEGIN
Select @pOut = 'Hello' + @pIn
END
GO
This is code-behind
string _connectString = @"Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True";
using(SqlConnection _connection = new SqlConnection(_connectString))
{
SqlCommand _command = new SqlCommand();
_command.Connection = _connection;
_command.CommandText = "prcTest";
_command.CommandType = CommandType.StoredProcedure;
SqlParameter[] _parameters = new SqlParameter[2];
_parameters[0] = new SqlParameter();
_parameters[0].ParameterName = "@pIn";
_parameters[0].SqlDbType = SqlDbType.NVarChar;
_parameters[0].Size = 50;
_parameters[0].Direction = ParameterDirection.Input;
_parameters[0].Value = "World";
_parameters[1] = new SqlParameter();
_parameters[1].ParameterName = "@pOut";
_parameters[1].SqlDbType = SqlDbType.NVarChar;
_parameters[1].Size = 50;
_parameters[1].Direction = ParameterDirection.Output;
_command.Parameters.AddRange(_parameters);
_connection.Open();
_command.ExecuteNonQuery();
_connection.Close();
Console.WriteLine(_command.Parameters["@pOut"].Value);
}
Thanks.....