PC Review


Reply
Thread Tools Rate Thread

Using Scope_Identity with a data adapter

 
 
Jason James
Guest
Posts: n/a
 
      18th Aug 2005
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.
 
Reply With Quote
 
 
 
 
W.G. Ryan MVP
Guest
Posts: n/a
 
      18th Aug 2005
Jason - check out "Managing an @@IDentity Crisis" by Bill Vaughn at
http://www.betav.com -> Articles -> MSDN
"Jason James" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
 
Jason James
Guest
Posts: n/a
 
      18th Aug 2005
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.

On Thu, 18 Aug 2005 09:10:00 -0400, "W.G. Ryan MVP"
<(E-Mail Removed)> wrote:

>Jason - check out "Managing an @@IDentity Crisis" by Bill Vaughn at
>http://www.betav.com -> Articles -> MSDN
>"Jason James" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> 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.

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i get SCOPE_IDENTITY from INSERT query using tableadapter. =?Utf-8?B?RGV2YW4=?= Microsoft C# .NET 1 19th Apr 2007 07:52 PM
Using Scope_Identity with a SQLDataSource in ASP.NET blouie Microsoft ASP .NET 0 5th Jan 2007 05:08 PM
Error while using Scope_Identity() instead of @@identity. WHY? nashak@hotmail.com Microsoft ADO .NET 1 26th Jan 2005 12:07 PM
Using @@IDENTITY and SCOPE_IDENTITY() =?Utf-8?B?bWljaGFlbA==?= Microsoft ADO .NET 2 26th Dec 2004 12:51 AM
Help using SCOPE_IDENTITY() Diego F. Microsoft ADO .NET 4 27th Aug 2004 01:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:50 PM.