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

  • Thread starter Thread starter MU
  • Start date Start date
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?
 
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(), ...);
 
Back
Top