Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

, 19 Dec 2012
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


Comments and Discussions

 
Questionthanks Pinmemberaleroy24-May-13 0:26 
AnswerRe: thanks PinmemberNitesh Luharuka24-May-13 5:35 
GeneralMy vote of 4 PinmemberSavalia Manoj M21-Jan-13 2:49 
GeneralMy vote of 5 PinmemberVitorHugoGarcia8-Jan-13 23:30 
GeneralRe: My vote of 5 PinmemberNitesh Luharuka24-May-13 5: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 | Mobile
Web01 | 2.8.140922.1 | Last Updated 19 Dec 2012
Article Copyright 2012 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid