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

Discussion in 'ASP .Net' started by MU, Jan 26, 2009.

  1. MU

    MU Guest

    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?
     
    MU, Jan 26, 2009
    #1
    1. Advertising

  2. MU

    Paul Shapiro Guest

    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(), ...);

    "MU" <> wrote in message
    news:...
    > 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?
     
    Paul Shapiro, Jan 26, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmF5RA==?=

    insert new record in detailsview - how to get master id

    =?Utf-8?B?SmF5RA==?=, Jul 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    5,803
    Richard Carpenter
    Jul 10, 2006
  2. deneushasler
    Replies:
    3
    Views:
    438
    deneushasler
    Jul 3, 2008
  3. Hanno

    Make new record current in DetailsView after Insert

    Hanno, Aug 5, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    212
    Hanno
    Aug 5, 2006
  4. Frits van Soldt

    How to insert a new record in detailsview when there are no record

    Frits van Soldt, Nov 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    810
    Frits van Soldt
    Nov 17, 2005
  5. Feege
    Replies:
    0
    Views:
    494
    Feege
    Dec 20, 2005
Loading...

Share This Page