DetailsView - How do I get the new record's ID after Insert?

M

MU

Hello,

I have a DetailsView on my form and I have a CheckBoxList that the
user can use in Insert mode. I need to take the values that were
selected from the CheckBoxList and insert them into another table
along with the NEW record id that is generated from the Insert from
the DetailsView.

How do I get access to the new record's ID after you click Insert so
that I can do the update to the other table? Is there a function that
I should use and what value do I check?

Thoughts?
 
P

Paul Shapiro

You retrieve the value (assuming your're using SQL Server) with
SCOPE_IDENTITY. From SQL BOL: "Returns the last identity value inserted into
an identity column in the same scope. A scope is a module: a stored
procedure, trigger, function, or batch. Therefore, two statements are in the
same scope if they are in the same stored procedure, function, or batch."

So either include "Select Scope_Identity() as newID;" in the batch following
your Insert statement, or include both Insert statements in the batch:
Insert Table1 (...);
Insert Table 2 (foreignKeyID, ...) Values (Scope_Identity(), ...);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top