Click here to Skip to main content
15,887,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am inserting data in sql using store procedure, its working fine but cmd.executenonquery returning -1 .
Can any one plz tell me that either it return -1 or there is any problem with my code?

Thanks in advance.
Posted
Updated 5-Sep-10 23:44pm
v2

Are you using inline queries. Check this link: http://msdn.microsoft.com/en-us/library/59x02y99(vs.71).aspx
 
Share this answer
 
Comments
Sushant Joshi 6-Sep-10 8:22am    
If you have inline queries you would get the updated row count, however, for stored proc, it is not possible to find the updated rows, hence the return value is -1
Have you got a trigger running on your table[^]?

"For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1."

If not, you might have an issue with the code.
 
Share this answer
 
Have a look here: ServerConnection.ExecuteNonQuery Method [^]

It says:
Return Value: An Int32 value that specifies the total number of rows affected by the Transact-SQL command for UPDATE, INSERT, and DELETE statements. For all other types of statements, the return value is -1.

Now, it depends on what are you doing. Though you say Inserting, but looks like not in a proper expected way.
 
Share this answer
 
ExecuteNonQuery in your case is not executing any command directly. It executes a stored procedure. Hence it will not return the number of rows affected. You will have to get that inside in your stored procedure and send it back as an out parameter.
 
Share this answer
 
Comments
C#ANDAN 7-Sep-10 15:01pm    
Reason for my vote of 4
precise
You ask if there is a problem with your code and then proceed to not show it.

Paste some code and it would help! :)
 
Share this answer
 
i have found the solution , hence i m using store procedure thats y it is returning -1 other wise it returns 1 in queries
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900