How can I retrieve an id from a row that I’ve just written to the database?
Basically, I have a function in my ASP.NET a booking form that needs to write to two tables, but I need the id from what’s been written in table 1, to also be store in table 2.
So:
- write to
table 1 - retrieve
idfromtable 1 - write to
table 2
How can I achieve this?
Regards
Tea
In Asp.Net with a Sql 2008 server, you can use the ExecuteScalar function like this:
OR
In the last example, notice I’m performing an INSERT and then a SELECT in the same request…
And to get the identity use: