Get ID of Saved Record in Button Click Handler


You can retrieve the ID of a newly created record in the SaveButton_Click() button handler method after the CommitTransaction() method executes and use that record ID in your code customization.
- Anh Trinh, Software Engineer, Iron Speed, Inc.

November 1, 2006
Iron Speed Designer V4.0

Get ID of Saved Record in Button Click Handler

Database schemas usually issue record ID’s by calling the newid() method or by using identity fields in your database. You can retrieve the ID of a newly created record in the SaveButton_Click() button handler method after the CommitTransaction() method executes and use that record ID in your code customization.

The example below uses an Add Orders Record page created from the Orders table in the Northwind database. The example saves an OrderID in a temporary ID variable, allowing you to add custom code that uses the OrderID.

Add your code customization to the Page class in the AddOrdersPage.aspx.cs file, typically located in:

...<Application Folder>\Orders\AddOrdersPage.aspx.cs or .vb

Add the code customization below to retrieve the new record’s ID.

C#:

public void SaveButton_Click(object sender, EventArgs args)
{
    bool shouldRedirect = true;
    try
    {
        DbUtils.StartTransaction();
        this.OrdersRecordControl.SaveData();
        this.CommitTransaction(sender);
 
        OrdersRecord rec = this.OrdersRecordControl.GetRecord();
        int tempId = rec.OrderID;
 
        //do more code customization
 
    }
    catch (Exception ex)
    {
        shouldRedirect = false;
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE", ex.Message);
        this.RollBackTransaction(sender);
    }
    finally
    {
        DbUtils.EndTransaction();
    }
    if (shouldRedirect)
    {
        this.ShouldSaveControlsToSession = true;
        this.RedirectBack();
    }
}

Visual Basic .NET:

Public Sub SaveButton_Click(ByVal sender As Object, ByVal args As EventArgs)
    Dim shouldRedirect As Boolean = true
    Try
        DbUtils.StartTransaction
        Me.OrdersRecordControl.SaveData
        Me.CommitTransaction(sender)
        Dim rec As OrdersRecord = Me.OrdersRecordControl.GetRecord
        Dim tempId As Integer = rec.OrderID
        'do more code customization
    Catch ex As Exception
        shouldRedirect = false
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE", ex.Message)
        Me.RollBackTransaction(sender)
    Finally
        DbUtils.EndTransaction
End Try
    If shouldRedirect Then
        Me.ShouldSaveControlsToSession = true
        Me.RedirectBack
    End If
End Sub

You may note this code customization is nearly identical to the SaveButton_Click_Base() method. So why can’t we simply add our custom code after calling the base method? The reason is that the SaveButton_Click_Base() method redirects to another page when it finishes its execution, so your code customization won’t be executed.

About the Author

Anh Trinh
Software Engineer, Iron Speed, Inc.

Anh is a software engineer at Iron Speed, Inc. He enjoys developing applications with Iron Speed Designer and Microsoft .NET technology.



  Privacy Statement