ASP.NET table insertion does not return the correct identifier value

In my Visual Studio 2008 ASP.NET codebehind webpage, I invoke the procedure for adding SQL tables to a table as follows.

NewLabID = LabItem.AddLaborItem(ThisWONum, TechID, DateTime.Now, DateTime.Now, "Working", ""); 

The record is added correctly, but "NewLabID" returns 1 each time, and not a new new LabID (LabID is defined as int / identity). Auto Generated Code Insert Command

 INSERT INTO [LaborDetail] ([WONum], [TechID], [StartDate], [StopDate], [OtherType], [OtherDesc]) VALUES (@WONum, @TechID, @StartDate, @StopDate, @OtherType, @OtherDesc); SELECT LabID FROM LaborDetail WHERE (LabID = SCOPE_IDENTITY()) 

and auto-generated code that calls this:

 returnValue = command.ExecuteNonQuery(); 

When doing this with the debugger, returnValue is always 1. Should the above command return the new value of the identification field?

+1
source share
1 answer

ExecuteNonQuery returns the number of rows affected. Instead, you will want to use ExecuteScalar ().

+3
source

Source: https://habr.com/ru/post/1336718/


All Articles