Why does ExecuteScalar () return null when my return values ​​are not null?

Attempt to assign result from procedure SQLto variable inC#

I have a procedure that has an input and output parameter. input inPara parameter accepts int Output parameter outPara returns bigint

When I test the procedure, neither the output parameter nor the return value reads null. (because I made it return the out parameter.)

This means that the procedure is not a problem, but something in mine C#.

My connection string and everything works correctly.

Code below:

SqlCommand c = new SqlCommand();

c.CommandText = "ProcedureName";
c.CommandType = CommandType.StoredProcedure;
c.Connection = con; //this works, prev defined

// Something below is the problem:
c.Parameters.Add(new SqlParameter("@inPara", 3));
SqlParameter outer = new SqlParameter("@outPara", SqlDbType.BigInt);
outer.Direction = ParameterDirection.Output;
c.Parameters.Add(outer);

object o = c.ExecuteScalar();

if (o != null) int i = (int) o;

but o always comes null , although it should not. Did I miss something?

As suggested in the comments, I show my SQL server stored in the procedure:

CREATE PROCEDURE MyStoredProcedure (@inPara int, @outPara bigint out )
AS
BEGIN

set @inPara = (Select A.ID from myTable A
INNER JOIN   
(SELECT ROW_NUMBER() OVER(ORDER BY HT_ID DESC) AS 'RN', *
  FROM myTable) B  ON A.ID= B.ID
AND B.RN = outPara)

return @outPara


END
GO

5 . outPara 1 5, @outpara

+4
1

, . ExecuteScalar, RETURN, SELECT.

SP, , '@outPara' ExecuteNonQuery().

UpdatedRowSource SqlCommand UpdateRowSource.OutputParameters, , .

command1.UpdatedRowSource = UpdateRowSource.OutputParameters;
command1.ExecuteNonQuery();

command1.Parameters["@outPara"].Value;

+4

Source: https://habr.com/ru/post/1608458/


All Articles