Is it possible to get scope_identity without a stored procedure?

  • Thread starter Thread starter jm
  • Start date Start date
J

jm

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.
 
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.
 
jm said:
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()
.......
 
Back
Top