I am new to writing Stored Procedure. So I wrote one with output parameters and want to access the output value, hot to do it.
My Stored Procedure:
ALTER PROCEDURE selQuery
(
@id int, @code varchar(50) OUTPUT
)
AS
SELECT RecItemCode = @code, RecUsername from Receipt where RecTransaction = @id
RETURN @code
If trying to set "@code=RecItemCode" getting error as : "A SELECT statement that assigns a value to a variable must not be combined with Data Retrieval operations."
And I am using the Stored Procedure as:
con.Open();
cmd.Parameters.AddWithValue("@id", textBox1.Text);
SqlParameter code = new SqlParameter("@code", SqlDbType.Int);
code.Direction = ParameterDirection.Output;
cmd.Parameters.Add(code);
SqlDataReader sdr = cmd.ExecuteReader();
MessageBox.Show(cmd.Parameters["@code"].Value.ToString()); // getting error
con.Close();
Error : "Object reference not set to an instance of an object." I want to get the value of output parameter. How to get that?
Thanks.
Let us see how to use Output Parameters in a Stored procedure to return multiple outputs. Here, we declare a variable called @LastName as input parameter, and three Output parameter. Within the procedure, we are finding the Occupation, Education, and yearly Income of an employee whose last name is equal to the input parameter.
Code language:SQL (Structured Query Language)(sql) Calling stored procedures with output parameters To call a stored procedure with output parameters, you follow these steps: First, declare variablesto hold the values returned by the output parameters Second, use these variables in the stored procedure call.
First, declare variablesto hold the values returned by the output parameters Second, use these variables in the stored procedure call. For example, the following statement executes the uspFindProductByModelstored procedure:
Having an output parameter means you want to return some value from the stored procedure and use it in ADF. In this example, the return value will control the next activities in the pipeline. Let's store the return value into a variable.
There are a several things you need to address to get it working
@ouput
its @code
AddWithValue
since its not supposed to have a value just you Add
.ExecuteNonQuery
if you're not returning rows Try
SqlParameter output = new SqlParameter("@code", SqlDbType.Int);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output);
cmd.ExecuteNonQuery();
MessageBox.Show(output.Value.ToString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With