Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / Visual Basic

Solution: ExecuteNonQuery() Always Returning -1 When Calling a Stored Procedure

Rate me:
Please Sign up or sign in to vote.
4.80/5 (5 votes)
19 Dec 2012CPOL 47.2K   6   5
A solution for ExecuteNonQuery() always returning -1 when calling a Stored Procedure

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.

VB.NET
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:

SQL
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)


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

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. #SOreadytohelp

Comments and Discussions

 
Questionthanks Pin
aleroy24-May-13 0:26
aleroy24-May-13 0:26 
AnswerRe: thanks Pin
Nitesh Kejriwal24-May-13 5:35
professionalNitesh Kejriwal24-May-13 5:35 
GeneralMy vote of 4 Pin
Savalia Manoj M21-Jan-13 2:49
Savalia Manoj M21-Jan-13 2:49 
GeneralMy vote of 5 Pin
_Vitor Garcia_8-Jan-13 23:30
_Vitor Garcia_8-Jan-13 23:30 
GeneralRe: My vote of 5 Pin
Nitesh Kejriwal24-May-13 5:33
professionalNitesh Kejriwal24-May-13 5:33 

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

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