how to get scope_identity here?

L

Luc

Hi,

i do an insert in the aspx file from a detailsview.
Now i need the 'id' of the record just inserted.
How can i do that?
I tried this, but the two sql statements are of course disconnected and it
doesn't work.
Thanks for help
Luc

code-behind
------------
Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles
DetailsView1.ItemInserted
Dim sql As String
Dim comd As SqlCommand
Dim id As Integer

Using mConnection As New SqlConnection(param.ConnectionString)
sql = "Select SCOPE_IDENTITY()"
comd = New SqlCommand(sql, mConnection)
mConnection.Open()
id = comd.ExecuteScalar
End Using
....

aspx file
 
M

Mr. Arnold

Luc said:
Hi,

i do an insert in the aspx file from a detailsview.
Now i need the 'id' of the record just inserted.
How can i do that?
I tried this, but the two sql statements are of course disconnected and it
doesn't work.
Thanks for help
Luc

code-behind
------------
Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles
DetailsView1.ItemInserted
Dim sql As String
Dim comd As SqlCommand
Dim id As Integer

Using mConnection As New SqlConnection(param.ConnectionString)
sql = "Select SCOPE_IDENTITY()"
comd = New SqlCommand(sql, mConnection)
mConnection.Open()
id = comd.ExecuteScalar
End Using
...

aspx file
etc ...
[/QUOTE]

Well, I don't think it's going to happen for you this way due to that fact
that you can only get the ID of the record inserted in a continuous in-line
execution of the code using one connection that contains the Insert and
Scope .

You need to use a Stored Proc to do this that returns the ID as output.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4419 (20090912) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
L

Luc

that's what i thought, thanks

Mr. Arnold said:

Well, I don't think it's going to happen for you this way due to that fact
that you can only get the ID of the record inserted in a continuous
in-line execution of the code using one connection that contains the
Insert and Scope .

You need to use a Stored Proc to do this that returns the ID as output.



__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4419 (20090912) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
[/QUOTE]
 
G

Gregory A. Beamer

Hi,

i do an insert in the aspx file from a detailsview.
Now i need the 'id' of the record just inserted.
How can i do that?
I tried this, but the two sql statements are of course disconnected
and it doesn't work.
Thanks for help
Luc

code-behind
------------
Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal
e As
System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles
DetailsView1.ItemInserted
Dim sql As String
Dim comd As SqlCommand
Dim id As Integer

Using mConnection As New SqlConnection(param.ConnectionString)
sql = "Select SCOPE_IDENTITY()"
comd = New SqlCommand(sql, mConnection)
mConnection.Open()
id = comd.ExecuteScalar
End Using
...

aspx file
-------
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:conn %>"
InsertCommand="INSERT INTO [mytable] ([field1],...) VALUES
(@field1,...)" > etc ...


You can try @@IDENTITY as the second command, but you could,
concievably, get the wrong answer that way. Best option is a stored
procedure to pull the SCOPE_IDENTITY().

Peace and Grace,
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top