Using Scope_Identity with a data adapter

J

Jason James

HI all,

I have built the SELECT, INSERT, UPDATE and DELETE SP
for my application. However, what I would like to be able to
do is return the SCOPE_IDENTITY value from the INSERT SP
to my VB.Net app. Can anyone tell me how to get the
SCOPE_IDENTITY value from the SP and put it back into the
datarow for the row that has just been added?

I have previously been using @@IDENTITY and executing
a cmd.ExecuteScalar to obtain the ID of the last inserted
record by handling the rowupdated event of the data adapter.

Like this:

Private Sub da_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.SqlClient.SqlRowUpdatedEventArgs) Handles da.RowUpdated
If e.StatementType = StatementType.Insert And e.Status =
UpdateStatus.Continue Then
Dim cmd As New SqlCommand("SELECT @@IDENTITY FROM
tblParts", conn)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim id As Int32 = cmd.ExecuteScalar()
conn.Close()
e.Row("iID") = id
ds.AcceptChanges()
End If
End Sub



Any thoughts would be greatly appreciated.

Many thanks,

Jason.
 
J

Jason James

Bill,

that's fantastic. Thanks for pointing me in the right direction.
I already had the JET technique sorted, but I was using it for
SQL2K as well. This will save some coding.

Kind regards,

Jason.
 

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

Top