Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteNonQuery() returns -1 always

Tags:

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?

like image 735
Lali Avatar asked Oct 17 '11 12:10

Lali


People also ask

What does ExecuteNonQuery () method return?

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.

Which of the following is the use of 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.

What is the return type of ExecuteScalar () method?

The ExecuteScalar method returns as a scalar value the value of the first column of the first row of the result set.

What is ExecuteNonQuery in VB net?

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.


1 Answers

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.

like image 75
Stuart Manning Avatar answered Oct 23 '22 16:10

Stuart Manning