UPDATE: my code is behaving as expected by there was a typo in the stored procedure that was the reason it was failing.
I can’t seem to figure out why or how to fix this because I am not getting any errors what I am getting is the return value is 0 which means fail.
Here is my .net code:
SqlParameter returnValue= new SqlParameter("returnValue", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
cmd.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value); //1 success and 0 failed
My stored procedure:
CREATE PROCEDURE EmployeeUpdate
@employee_id BIGINT,
@name nvarchar(250)
AS
BEGIN
SET NOCOUNT ON
DECLARE @Result int
SET @Result = 0
UPDATE Employee
SET name = @name
WHERE employee_id = @employee_id
IF (@@rowcount = 1)
BEGIN
SET @Result = 1
END
SET NOCOUNT OFF
RETURN @Result
END
So if I just execute the stored procedure from SQL Server Management Studio, it does update my row successfully without any error
EXEC EmployeeUpdate 34,'John John'
Return Value = 1
I would suggest to remove returnValue parameter altogether and just use the return value of ExecuteNonQuery() method instead:
Stored procedure: