Dear friends,
I want to know does stored procedure in sql server always returns a value?
pls reply
thanks
aamir
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Oct 3, 2011, 3:09 AM
If you use ExecuteReader, the Return values are stores as rows and columns. You have to loop through to reader to fetch data.
You can't use ExecuteScalar to get the returned value, and ExecuteNonQuery will always return -1. To get the value back, you need to add a return value parameter to the command.
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(storedProcedureName, connection);
command.CommandType = CommandType.StoredProcedure;
// Return value as parameter
SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
// Execute the stored procedure
connection.Open();
command.ExecuteNonQuery(); // Return value will be stored in returnValue
connection.Close();
-----------------------------------------------------------------------------------------
If this post helps you, then mark as "Correct Answer"
Thank You