Incorrect return value when using ExecuteNonQuery()
c#, sql, sql-server
Solution
Because you turned it off:
SET NOCOUNT ON;
Problem
I have a C# program I'm writing which interacts with SQL Server. I had foolishly hard-coded the query strings into the C# program, and wanted to instead turn those into Stored Procedures on the server. For some reason, one particular INSERT statement (that was working fine!) no longer works and I can't figure out why. I've even run a SQL Profiler Trace, and it shows that the statement is perfectly formed! Maybe someone can tell me what I'm doing wrong. STORED PROCEDURE: This SP just takes a bunch of parameters and inserts them into a table. Very simple. ``` ALTER PROCEDURE [dbo].[usp_InsertNewChangeRequest] @Requester INT, @ChangeCreationDate DATETIME, @ChangeName VARCHAR(200), @ChangeDescription VARCHAR(1000), @LastModifiedDate DATETIME, @LastModifiedBy INT, @AffectedArea INT, @ImplementationPlan VARCHAR(MAX), @BackoutPlan VARCHAR(MAX), @RiskLevel TINYINT, @ApprovalRequired BIT, @IsApproved BIT AS BEGIN SET NOCOUNT ON; INSERT INTO dbo.ChangeRequests(Requester, ChangeCreationDate, ChangeName, ChangeDescription, LastModifiedDate, LastModifiedBy, AffectedArea, ImplementationPlan, BackoutPlan, RiskLevel, ApprovalRequired, IsApproved) VALUES (@Requester, @ChangeCreationDate, @ChangeName, @ChangeDescription, @LastModifiedDate, @LastModifiedBy, @AffectedArea, @ImplementationPlan, @BackoutPlan, @RiskLevel, @ApprovalRequired, @IsApproved) END ``` My code in C# simply preps the SP parameters then calls a nonquery: ``` public int InsertNewChange(int RequesterID, DateTime CreationDate, string ChangeName, string ChangeDescription, DateTime LastModifiedDate, int AffectedAreaID, string ImplementationPlan, string BackoutPlan, int RiskLevel, int ApprovalRequired, int IsApproved) { int retval = 0; // Create a command whose name is the stored procedure for inserts SqlCommand command = new SqlCommand("usp_InsertNewChangeRequest", scConnection); command.CommandType = CommandType.StoredProcedure; // add the parameters to the stored procedure command.Parameters.Add(new SqlParameter("@Requester", RequesterID)); command.Parameters.Add(new SqlParameter("@ChangeCreationDate", CreationDate)); command.Parameters.Add(new SqlParameter("@ChangeName", ChangeName)); command.Parameters.Add(new SqlParameter("@ChangeDescription", ChangeDescription)); command.Parameters.Add(new SqlParameter("@LastModifiedDate", LastModifiedDate)); command.Parameters.Add(new SqlParameter("@LastModifiedBy", RequesterID)); command.Parameters.Add(new SqlParameter("@AffectedArea", AffectedAreaID)); command.Parameters.Add(new SqlParameter("@ImplementationPlan", ImplementationPlan)); command.Parameters.Add(new SqlParameter("@BackoutPlan", BackoutPlan)); command.Parameters.Add(new SqlParameter("@RiskLevel", RiskLevel)); command.Parameters.Add(new SqlParameter("@ApprovalRequired", ApprovalRequired)); command.Parameters.Add(new SqlParameter("@IsApproved", IsApproved)); retval = command.ExecuteNonQuery(); return retval; } ``` I'm constantly getting a -1 return value, whereas before, when I had the SQL text spelled out entirely in C#, I would get a 1, for the 1 row inserted. Infuriatingly, I don't see any error in SQL Profiler when doing a trace. It shows the statement passed, and it looks completely fine! When I cut and paste it into Management Studio and run it myself by hand, it works fine. ``` exec usp_InsertNewChangeRequest @Requester=4,@ChangeCreationDate='2012-05-16 17:55:45',@ChangeName='test name',@ChangeDescription='test description',@LastModifiedDate='2012-05-16 17:56:01.937',@LastModifiedBy=4,@AffectedArea=2,@ImplementationPlan='test implem',@BackoutPlan='test backout',@RiskLevel=1,@ApprovalRequired=0,@IsApproved=0 ``` Does anyone know why this would be happening to me? MANY THANKS!