May 12, 2019

ExecuteNonQuery() always returns -1

I created the following simple SP to update table record. While running this SP from C#, the ExecuteNonQuery() method was constantly returning -1.

CREATE PROCEDURE [dbo].[UpdateCategory]
  @CategoryID int
 ,@CategoryCode varchar(10)
 ,@CategoryName varchar(50)
 ,@CategoryDescription varchar(100)

AS
BEGIN
 
 SET NOCOUNT ON;

    UPDATE [dbo].[Category]
    SET 
        [CategoryCode] = @CategoryCode
        ,[CategoryName] = @CategoryName
        ,[CategoryDescription] = @CategoryDescription

  WHERE CategoryID = @CategoryID

END

After searching I found the issue was the first line of SP body.

SET NOCOUNT ON;

Setting NOCOUNT ON will stop the number of affect rows to be returned to client. So the default response is always -1.

To get the response with real no of affected rows just remove this line and it will start returning the number of affected rows.

No comments:

Post a Comment