1

I have a simple c# application that uses the Entity Framework 5.0 and should call a stored procedure with output parameters. The problem is that it doesn't work, and instead, it returns -1.

The c# code that I have looks like that:

// Define the output paramaters
SqlParameter operatorID = new SqlParameter("@operatorID", SqlDbType.Int);
operatorID.Direction = ParameterDirection.Output;
SqlParameter operatorCode = new SqlParameter("@operatorCode", SqlDbType.Int);
operatorCode.Direction = ParameterDirection.Output;

var parameters = new List<object>();
parameters.Add(operatorID);
parameters.Add(operatorCode);

var noOutput = context.Database.ExecuteSqlCommand("EXEC my_sp_name @operatorID, @operatorCode", parameters.ToArray());

The stored procedure looks like that:

CREATE PROCEDURE [dbo].[my_sp_name] 
( 
     @operatorID        int             OUTPUT 
    ,@operatorCode      int             OUTPUT
 )
AS
    SET @operatorID = 123

I know it doesn't make sense, but it's just for a test.

The problem after executing the c# code is that the noOutput value is -1 and the operatorID value is empty, where I expect it to be 123.

Does anybody have an idea what I do wrong?

2 Answers 2

6

Your parameters in query must be with OUTPUT keyword

var noOutput = context.Database.ExecuteSqlCommand("EXEC my_sp_name @operatorID OUTPUT, @operatorCode OUTPUT", parameters.ToArray());
Sign up to request clarification or add additional context in comments.

3 Comments

Oh my GOD, are you kidding me? Thanks a lot! Adding the OUTPUT works fine, but I thought that it's not necessary since I have already defined them as output parameters.
You must write OUTPUT.I have the same issue in MSSQL in query with OUTPUT Keyword
I understand now. I'll accept the answer in 4 minutes.
0

Check the "operatorCode.Value" property. the result will be there after execution.

If not try to use the CommandType.StoredProcedure and then execute the command with the store procedure name along with the parameters. you will get the output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.