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;
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
user5362858