Is it possible to get scope_identity without a stored procedure?

Discussion in 'ASP .Net' started by jm, Oct 23, 2006.

  1. jm

    jm Guest

    I am using the generated SQL from VS using the DAL. After my INSERT it
    has the select...scope_identity().

    Is there any way to get the value of my new id field from this select
    after the insert?

    I thought it might be using the iteminserted even or the iteminserting
    event, but I can't figure it out.

    Do I have to write a custom insert and use the BLL and return a scalar?
    If so where do I get that scalar from (what event is it wrapped in)?

    Thank you for any help.
    jm, Oct 23, 2006
    #1
    1. Advertising

  2. Yes, it is possible. All you need to do is to run the insert and the select
    statement in the same batch:

    insert ...;select ...scope_identity()

    Run is with ExecuteScalar method.
    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "jm" <> wrote in message
    news:...
    >I am using the generated SQL from VS using the DAL. After my INSERT it
    > has the select...scope_identity().
    >
    > Is there any way to get the value of my new id field from this select
    > after the insert?
    >
    > I thought it might be using the iteminserted even or the iteminserting
    > event, but I can't figure it out.
    >
    > Do I have to write a custom insert and use the BLL and return a scalar?
    > If so where do I get that scalar from (what event is it wrapped in)?
    >
    > Thank you for any help.
    >
    Eliyahu Goldin, Oct 24, 2006
    #2
    1. Advertising

  3. jm

    jm Guest

    jm wrote:
    > I am using the generated SQL from VS using the DAL. After my INSERT it
    > has the select...scope_identity().
    >
    > Is there any way to get the value of my new id field from this select
    > after the insert?
    >
    > I thought it might be using the iteminserted even or the iteminserting
    > event, but I can't figure it out.
    >
    > Do I have to write a custom insert and use the BLL and return a scalar?
    > If so where do I get that scalar from (what event is it wrapped in)?
    >
    > Thank you for any help.


    I was able to get the value from the Inserted event of the
    ObjectDataSource:

    protected void ObjectDataSource1_Inserted(object sender,
    ObjectDataSourceStatusEventArgs e)
    {
    int employeeid = Convert.ToInt32(e.ReturnValue); //get the new
    employeeid for this scope_identity()
    .......
    jm, Oct 25, 2006
    #3
    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. Guoqi Zheng
    Replies:
    5
    Views:
    475
    Scott Allen
    Oct 31, 2004
  2. Replies:
    3
    Views:
    885
  3. Mike P
    Replies:
    0
    Views:
    3,295
    Mike P
    Jun 19, 2006
  4. jobs
    Replies:
    0
    Views:
    877
  5. Luc
    Replies:
    3
    Views:
    1,383
    Gregory A. Beamer
    Sep 14, 2009
Loading...

Share This Page