Data Access has been discontinued. Please refer to this page for more information.

Performing Update Operations

With the OpenAccessLinqDataSource control, you can create ASP.NET Web pages that enable users to insert, update, and delete data. You don't have to write any additional code, because the OpenAccessLinqDataSource control dynamically performs these operations. To let users modify data, you can enable insert, update, and delete operations on the OpenAccessLinqDataSource control. You can then connect the control to a data-bound control that lets the users insert, update, and delete data, such as the RadGrid control.

This topic describes how to enable the OpenAccessLinqDataSource control to automatically handle update operations. It also describes how to set properties programmatically before an update operation is performed. Finally, the topic makes an overview of the Updating/Updated events, exposed by the OpenAccessLinqDataSource control.

How to: Enable Automatic Updates

To enable a data-bound control to automatically perform update operations, you must do the following:

  1. Connect the OpenAccessLinqDataSource control to a context class and specify the target table. To do that you need to set values for the ContextTypeName, and ResourceSetName properties:

    <telerik:OpenAccessLinqDataSource 
        ID="OpenAccessLinqDataSourceCategory" 
        runat="server" 
        ContextTypeName="SofiaCarRental.Model.FluentModel" 
        ResourceSetName="Categories">
    </telerik:OpenAccessLinqDataSource>
    
  2. Set the EnableUpdate property to True:

    <telerik:OpenAccessLinqDataSource 
        ID="OpenAccessLinqDataSourceCategory" 
        runat="server" EnableUpdate="True"
        ContextTypeName="SofiaCarRental.Model.FluentModel" 
        ResourceSetName="Categories">
    </telerik:OpenAccessLinqDataSource>
    

    The control should not be set up to execute any GroupBy operations, or to retrieve a subset of the class properties.

Using the Updating/Updated Events

The Updating event occurs before an update operation. You could handle the Updating event to validate the object to be updated, to change a value before the update operation, or to cancel the update operation. The OpenAccessLinqDataSourceUpdateEventArgs object passed to the event handler for this event contains the object that will be updated. The OpenAccessLinqDataSourceUpdateEventArgs object also exposes information about validation errors. For example, a validation error occurs if a value to be updated does not match the type of the property in the domain class. You can retrieve the validation exceptions via the Exception property and take appropriate actions. For example:

protected void OpenAccessLinqDataSourceCategory_Updating(object sender, 
    Telerik.OpenAccess.Web.OpenAccessLinqDataSourceUpdateEventArgs e)
{
   if (e.Exception != null)
   {
       foreach (KeyValuePair<string, Exception> innerException in
               e.Exception.InnerExceptions)
       {
           // Process the exception
       }
       e.ExceptionHandled = true;
   }
}
Protected Sub OpenAccessLinqDataSourceCategory_Updating(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceUpdateEventArgs) _
    Handles OpenAccessLinqDataSource.Updating
 If e.Exception IsNot Nothing Then
  For Each innerException As KeyValuePair(Of String, Exception) In e.Exception.InnerExceptions
   ' Process the exception
  Next innerException
  e.ExceptionHandled = True
 End If
End Sub

The Updating event allows you to modify data before the update operation. The following example demonstrates how to do this. The OriginalObject property contains information about the values that were originally retrieved from the data source. The NewObject property returns the object that contains values that will be saved in the database. Note that both of the properties are of type object. That's why, you need to cast them to the types that represent the table.

protected void OpenAccessLinqDataSourceCategory_Updating(object sender,
    Telerik.OpenAccess.Web.OpenAccessLinqDataSourceUpdateEventArgs e)
{
   Category originalCategory = e.OriginalObject as Category;
   Category newCategory = e.NewObject as Category;
   if (newCategory.CategoryName != originalCategory.CategoryName)
   {
       newCategory.CategoryName = originalCategory.CategoryName;
   }
}
Protected Sub OpenAccessLinqDataSourceCategory_Updating(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceUpdateEventArgs) _
    Handles OpenAccessLinqDataSource.Updating
 Dim originalCategory As Category = TryCast(e.OriginalObject, Category)
 Dim newCategory As Category = TryCast(e.NewObject, Category)
 If newCategory.CategoryName <> originalCategory.CategoryName Then
  newCategory.CategoryName = originalCategory.CategoryName
 End If
End Sub

The OpenAccessLinqDataSourceUpdateEventArgs derives from CancelEventArgs, i.e. it is a cancelable event. You can cancel the update operation by setting the Cancel property to True.

protected void OpenAccessLinqDataSourceCategory_Updating(object sender, 
    Telerik.OpenAccess.Web.OpenAccessLinqDataSourceUpdateEventArgs e)
{
   e.Cancel = true;
}
Protected Sub OpenAccessLinqDataSourceCategory_Updating(ByVal sender As Object, _
    ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceUpdateEventArgs) _
    Handles OpenAccessLinqDataSource1.Updating
 e.Cancel = True
End Sub

The Updated event occurs when an update operation has finished. You could handle the Update event to catch any backend exceptions from the update operation or to examine the values after the update operation has finished. To access the updated object, you need to examine the Result property of the OpenAccessLinqDataSourceStatusEventArgs class.