Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Solution: ExecuteNonQuery() always returning -1 when calling a Stored Procedure

, 19 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Friends, This post details another solution to a very basic problem that we face in our development and tend to miss writing Stored Procedures. We know ExecuteNonQuery() function defined in the SqlCommand class returns the number of rows affected by the query we are executing. Sometimes, when callin

Friends,

This post details another solution to a very basic problem that we face in our development and tend to miss writing Stored Procedures. We know ExecuteNonQuery() function defined in the SqlCommand class returns the number of rows affected by the query we are executing. Sometimes, when calling a Stored Procedure using the ExecuteNonquery() from .NET code returns a value of -1.

Let us consider this function that tries inserting Email address and Password fields to the database via a test SP.

Public Function TestInsert() As Boolean
Dim conn As SqlConnection = Nothing
Dim cmd As SqlCommand = Nothing
Try
    conn = New SqlConnection(connectionString)
    conn.Open()
    If passport Is Nothing Then
          cmd = New SqlCommand("sp_Test_Insert", conn)
    End If
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.Add(New SqlParameter("Email", "email@email.com"))
    cmd.Parameters.Add(New SqlParameter("Password", "password"))
    Dim rows = cmd.ExecuteNonQuery()
   If rows > -1 Then
    Return True
   Else
    Return False
   End If
Catch ex As Exception
   Return False
Finally
   If Not conn Is Nothing Then
      conn.Close()
   End If
   conn = Nothing
End Try
End Function

When you generate a Stored Procedure in SSMS using the “New Stored Procedure” link, SQL generates a template for you. Below is a modified version of the same being used in our code above -

CREATE PROC sp_Test_Insert
@Email nvarchar(255),
@Password nvarchar(20)
AS
BEGIN
   SET NOCOUNT ON;
   INSERT INTO Test_Table(Email, Password) VALUES(@Email, @Password)
END

The query looks fine and after doing a bit of research, I found that the 1st line in the Stored procedure is the culprit. From MSDN, placing SET NOCOUNT ON;  in the query sets, prevent extra result sets from SQL server interfering with SELECT statements.

To resolve, just remove this line or change this line to SET NOCOUNT OFF; and everything works fine.

Hope you enjoyed reading this. Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nitesh Luharuka
Team Leader Independent IT Professional, Consultant
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.
 
I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
Questionthanks Pinmemberaleroy24-May-13 1:26 
AnswerRe: thanks PinmemberNitesh Luharuka24-May-13 6:35 
GeneralMy vote of 4 PinmemberSavalia Manoj M21-Jan-13 3:49 
GeneralMy vote of 5 PinmemberVitorHugoGarcia9-Jan-13 0:30 
GeneralRe: My vote of 5 PinmemberNitesh Luharuka24-May-13 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 19 Dec 2012
Article Copyright 2012 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid