I am trying to complete a seemingly simple task that has turned into a several hour adventure: Getting @@Identity
from TableAdapter.Insert()
.
Here's my SQL Code that works.
CREATE PROCEDURE [dbo].[Branch_Insert]
(
@UserId uniqueidentifier,
@OrganisationId int,
@InsertedID int OUTPUT
)
AS
SET NOCOUNT OFF;
INSERT INTO [Branch] ([UserId], [OrganisationId])
VALUES (@UserId, @OrganisationId);
SELECT Id, UserId, OrganisationId FROM Branch WHERE (Id = SCOPE_IDENTITY())
SELECT @InsertedID = SCOPE_IDENTITY()
Then when I create the Table Adapter - I can instantly see the @InsertedID parameter.
Then from code, all I do is:
int? insertedId = 0;
branchTA.Insert(userId, orgId, ref insertedId);
I'm not 100% whether using ref is the best option but this works for me.
Good luck.