I have a stored procedure and it returns a string value, and I have to execute this SP using ExecuteNonQuery
statement. When I execute this, I get -1 as the return value.
I never used SET NOCOUNT ON
. How can I get the return value? I did not declare the return value as OUTPUT
-- just SELECT @VARIABLENAME
. How can I get the value using ExecuteNonQuery
?
ExecuteScalar() Method:ExecuteScalar() method is used to retrieve a single value from database. It executes the defined query and returns the value in the first column of the first row in the selected result set and ignores all other columns and rows in the result set.
Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. This requires less code than using the ExecuteReader method, and then performing the operations that you need to generate the single value using the data returned by a SqlDataReader.
You have to assign the connection for the command object. Your code is vulnerable to SQL injection. Also, you should use a using block with classes that implement the IDisposable interface, especially with the SqlConnection class in order to close the connection in case of an exception.
ExecuteNonQuery method is used to execute SQL Command or the storeprocedure performs, INSERT, UPDATE or Delete operations. It doesn't return any data from the database. Instead, it returns an integer specifying the number of rows inserted, updated or deleted.
ExecuteNonQuery
is used to execute queries that return only the number of rows affected (though output parameters can be populated). -1
means that no rows were affected or you have set nocount on.
Use another an API call if you need to read data from the sproc.
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