How do I record the primary key of an inserted record

The table must have a PrimaryKey

Note that the best way to do this is by creating a store procedure that returns the Primary Key
Make sure your SQLDataSource has the output parameter:


InsertCommand="INSERT INTO [Orders] ([UserID], [Total], [OrderDate]) 
VALUES (@UserID, @Total, {fn NOW()}); Select @OrderID = @@Identity;"



                
                
                
                
            
            
 

On your code behind have the following

**************

            
Dim ordernumber as integer

Protected Sub dsOrders_Inserted(ByVal sender As Object, ByVal e 
As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles 
dsOrders.Inserted
        ordernumber = e.Command.Parameters("@OrderID").Value
'Make sure your database has an identity value as your primary key
'if your database does not have primary keys this method will not work.
    End Sub

Related Posts

No Comments Yet.

Leave a reply

You must be logged in to post a comment.