Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Inserted ID

Status
Not open for further replies.

AdmanOK

MIS
Mar 5, 2001
412
0
0
I have a page that adds a new record to a SQL database with default parameters, and then redirects to a page where you can then modify and change that record. When I insert the record is there any way to get the autogenerated ID that was added? Currently I tell it to select the top record, but if multiple users are using the application at the same time it can cause conflicts. I have similar applications running on MS SQL and on MySQL so suggestions for either would be appreciated.
 
Are you using a stored procedure? You can return the identity of the inserted using scope_identity(). You can then reurn that value as an output parameter to your page.
 
I did something similar and what I did was re-query the DB on my second page using stored variables from the first page so I could get the ID field from that newly created record, so like jbenson001 said, you should be able to do that without any problem...
 
Hi,

Yep, set a parameter to output like:-

Dim parameterNewid As New SqlParameter("@Newid", SqlDbType.Int)
parameterNewid.Direction = ParameterDirection.Output
command.Parameters.Add(parameterNewid)

you can then call

set @NewID=@@Identity

from the end of your stored procedure.

This brings back the inserted unique value.

hth
j


----------------------------------------------------------------------------------------
A community development project in asp.net -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top