I am using a stored procedure to insert some value in table.
CREATE PROCEDURE [dbo].[Sp_InsertValue] @Val1 as nvarchar(50) @Val2 as nvarchar(50) as BEGIN IF NOT EXISTS(SELECT * FROM @mytable WHERE ID=@Val1) INSERT INTO @mytable VALUES(@VAL2) END
I am using ExecuteNonQuery() to call this stored procedure in ASP.NET using C#. It works fine, no issues, it inserts values if they don't exist. The issue is that cmd.ExecuteNonQuery() always return -1. I expect if a record is inserted, it should return 1, and 0 if not, right?
The ExecuteNonQuery method returns an integer that represents the number of rows affected by the statement or stored procedure that was executed. If multiple statements are executed, the value returned is the sum of the records affected by all of the statements executed.
ExecuteNonQuery used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.
The ExecuteScalar method returns as a scalar value the value of the first column of the first row of the result set.
Vb.NET ExecuteReader and ExecuteNonQuery ExecuteNonQuery : ExecuteNonQuery used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.
Check that you don't have "SET NOCOUNT ON" in your stored procedure. This will stop the number of affect rows be returned. Literally 'NoCount' is ON.
Default response will always be '-1' in this situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With