Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Retrieve newly inserted record ID?

Reply
Thread Tools

Retrieve newly inserted record ID?

 
 
11D.Universe
Guest
Posts: n/a
 
      06-11-2007
I'm using the Insert button in a DetailsView to insert a record into a
SQL table. I've got part of what I need to recover the record ID...

The INSERT COMMAND is:
InsertCommand="INSERT INTO [inventory] ([Active], ...) VALUES
(@Active...); select SCOPE_IDENTITY();"

The Select Command is:
SelectCommand="SELECT * FROM [inventory] where [inventoryID] =
@identity ORDER BY [InventoryID] DESC"

The Insert Parameters are:
<InsertParameters>
<asparameter Name="Active" Type="Boolean" />
...
<asp:QueryStringParameter Name="@identity"
Direction="Output" Type="Int32" />
</InsertParameters>

The Select Parameters are:
<asparameter Name="identity" Type="Int32" />

I know I'm supposed to put some code in the SqlDataSource1_Inserted
sub, but not sure what code is needed to get the InventoryID from the
inserted record.

Am I on the right track??

Any help would be appreciated...

 
Reply With Quote
 
 
 
 
Riki
Guest
Posts: n/a
 
      06-12-2007

"11D.Universe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> I'm using the Insert button in a DetailsView to insert a record into a
> SQL table. I've got part of what I need to recover the record ID...
>
> The INSERT COMMAND is:
> InsertCommand="INSERT INTO [inventory] ([Active], ...) VALUES
> (@Active...); select SCOPE_IDENTITY();"
>
> The Select Command is:
> SelectCommand="SELECT * FROM [inventory] where [inventoryID] =
> @identity ORDER BY [InventoryID] DESC"
>
> The Insert Parameters are:
> <InsertParameters>
> <asparameter Name="Active" Type="Boolean" />
> ...
> <asp:QueryStringParameter Name="@identity"
> Direction="Output" Type="Int32" />
> </InsertParameters>
>
> The Select Parameters are:
> <asparameter Name="identity" Type="Int32" />
>
> I know I'm supposed to put some code in the SqlDataSource1_Inserted
> sub, but not sure what code is needed to get the InventoryID from the
> inserted record.
>
> Am I on the right track??


Yes, you are.

Put this code in SqlDataSource1_Inserted (VB.NET):

If (e.Exception IsNot Nothing) Then
' log error or show error
e.ExceptionHandled = True
Return
End If
Dim InventoryID As Integer =
CInt(e.Command.Parameters("@identity").Value)

You may have to modify your INSERT command to match the parameter:
select SCOPE_IDENTITY AS @identity;

Riki


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
ObjectDataSource problem with return newly inserted record id fig000 ASP .Net 0 09-06-2008 10:32 PM
Retrieving the value of the identity field of a newly inserted record? Craig ASP .Net 1 01-25-2006 02:21 AM
Retrieving the value of the identity field of a newly inserted record? Craig ASP .Net Web Controls 0 01-02-2006 09:12 PM
Win XP Sp1 CD ROM and DVD Rom Not Recognizing Newly inserted discs Hunter Computer Support 5 12-12-2003 05:33 AM
Win XP Sp1 CD ROM and DVD Rom Not Recognizing Newly inserted discs Hunter Computer Support 4 10-14-2003 05:38 AM



Advertisments