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 protected]"))
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

Join the conversation! Your thoughts help the community grow.