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);
开发者_C百科 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!
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 @@Identity
see @@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.
精彩评论