Tom Gullen
Tom Gullen

Reputation: 61755

ASP.net DAL get ID of record just inserted

        // Add into DB
        int recordID = 0;
        using (tblArtworkTemplatesTableAdapter tblAdapter = new tblArtworkTemplatesTableAdapter())
        {
            tblAdapter.Insert(DateTime.Now, int.Parse(lstChooseSpec.SelectedValue), Master.loginData.loggedInUser.ID);
            recordID = int.Parse(tblAdapter.GetLastID().ToString());
        }

        // Redirect
        Response.Redirect("artworkDesigner.aspx?ID=" + recordID);

The stored procedure it's calling is:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetLastID]
AS
    SET NOCOUNT ON;
select @@identity FROM tblArtworkTemplates

I can't seem to get it to work, I'm a newb with this DAL stuff any help appreciated!

Upvotes: 0

Views: 1093

Answers (1)

DaveB
DaveB

Reputation: 9530

I am guessing that this is SQL Server? You should return the ID of the newly inserted record from your INSERT INTO stored procedure. For more on @@Identitysee @@IDENTITY (Transact-SQL)

Return the ID of the newly inserted record from your stored procedure:

SELECT ID AS LastID FROM tblArtworkTemplates WHERE ID = @@Identity; 

Fore more on the topic see How To Get Last Inserted ID On SQL Server.

Upvotes: 1

Related Questions