Is it possible to get scope_identity without a stored procedure?

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.
 
E

Eliyahu Goldin

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.
 
J

jm

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

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top