views:

626

answers:

1

In my VS2008 asp.net web page codebehind I am calling an add routine in a 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);\r\nSELECT 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?

+2  A: 

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

Tchami
Thanks - but how do I get asp.net to autogenerate ExecuteScalar instead of ExecuteNonQuery? The autogenerated command is clearly designed to return the new ID.
dsteele
OK, I got it - I had to change the ExecuteMode property of the query in the table adapter.
dsteele
You beat me to it. Glad it worked out for you.
Tchami