A1VBCode Forums

How to retrieve identity column value after inserting data into the table?


http://www.a1vbcode.com/vbforums/Topic26757.aspx

By johndevid0987 - 1/17/2009

I am doing an asp.net using C# to program an application which accept user input and save into database then it requires approval. There is a colum in table that approved_or_not. So this purpose i created another page where admin can login to enter the id number which is generated by DB to approve or reject. So every time user input data and submit the application needs to send the identity colum nuber (PK) as an email to admin.
By TallOne - 1/19/2009

You'll need an output parameter in the add stored procedure.  You add this parameter in c# code same as others but define it as type output.  When executing your command object it will look something like this

lMyLongValue = command.executeScalar;

This will set your variable to the the return value of the stored procedure.

Your stored procedure will include something like this:

DECLARE @ReturnValue AS INTEGER

INSERT INTO xTABLE....

SET @ReturnValue = @@Identity
SELECT @ReturnValue

Post your code and I'll give you hand if needed.

Good Luck