dsteele
dsteele

Reputation: 1272

ASP.NET tableadapter insert not returning the correct identity value

In my Visual Studio 2008 ASP.NET web page codebehind, I am calling an add routine in an SQL tableadapter, as follows.

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

The record is being added correctly, but 'NewLabID' returns 1 every time, not the actual new LabID (LabID is defined as int/identity). The autogenerated code insert command is

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 the autogenerated code which calls this is:

returnValue = command.ExecuteNonQuery();

Stepping through this with the debugger, returnValue is always 1. Shouldn't the above command return the new identity field value?

Upvotes: 1

Views: 3176

Answers (1)

Tchami
Tchami

Reputation: 4787

ExecuteNonQuery returns the number of rows affected. You will want to use ExecuteScalar() instead.

Upvotes: 3

Related Questions