I have looked into this issue before I posted, and found some solutions which I have tried to apply, but no luck in it working.
I have a stored procedure to insert records into a database. I want to return a value as a status (I have seen that you can only return an int on a RETURN and that is fine in this case).
ALTER PROCEDURE [dbo].[_example]
-- Add the parameters for the stored procedure here
@Parameters)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Example int
DECLARE @retVal int = 0
IF (@example is null)
BEGIN
SET @retVal +=1;
END
return @retVal
END
in my c# code I have written this:
public static int ExecuteSPWithReturnValue(string queryString)//not returning the value correctly
{
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection)
{
CommandTimeout = 0
};
SqlParameter retVal = command.Parameters.Add("@retVal", SqlDbType.Int);
retVal.Direction = ParameterDirection.ReturnValue;
command.ExecuteNonQuery();
int result = (int)retVal.Value;
connection.Close();
return result;
}
I call it from another class like this:
int returnStatus;
returnStatus = SQLUtility.ExecuteSPWithReturnValue("EXEC dbo._example " +
"@param1= '" + string + "'," +
"@param2= '" + string + "'," +
"@param3 = '" + string + "'," +
"@param4 ='" + string + "'");
but it just returns 0 no matter what. I know the value of the parameter is between 0-3, but that's not being passed into the int return Status.
Why?