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

Performing Update Operations

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

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 domain model and specify the target table.
  2. On the second page of the wizard (i.e. the Configure Data Selection page), you need to ensure that the following conditions are met:

    1. You need to select all (*) properties for the specified entity. When you check the Select All (*) option, full entities are returned instead of results that contain individual properties. (*) is the default selection, i.e. all of the properties will be used. If you choose specific properties, you won't get an entity object back. Instead, you will get a projection, which cannot be change-tracked, and therefore cannot be updated. In this case automatic inserts, updates, and deletes are not allowed.

    2. Switch to the Advanced tab page and check the Enable Automatic Updates option.

    3. You should not have any GroupBy operations.

  3. Click Finish to close the wizard.

If you have already created an OpenAccessLinqDataSource control on your web page, you don't need to re-configure it:

  1. Select the control in design time.
  2. By using the Smart Tag panel, check the Enable update option.

    Note that, if you haven't used the Select All (*) option or you have grouping, then the Enable delete/insert/update options in the Smart Tag panel will not be available.

The following example shows the markup for part of an ASP.NET Web page that contains an OpenAccessLinqDataSource. The control is configured to enable the user to display and update data in a table named Categories.

<telerik:OpenAccessLinqDataSource
   ID="OpenAccessLinqDataSource1"
   runat="server"
   ContextTypeName="OpenAccessLinqDataSourceDemo.EntitiesModel"
   ResourceSetName="Categories"
   EntityTypeName=""
   EnableUpdate="True" />

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 OpenAccessLinqDataSource_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 OpenAccessLinqDataSource_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 OpenAccessLinqDataSource_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 OpenAccessLinqDataSource_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 OpenAccessLinqDataSource_Updating(object sender, 
    Telerik.OpenAccess.Web.OpenAccessLinqDataSourceUpdateEventArgs e)
{
   e.Cancel = true;
}
Protected Sub OpenAccessLinqDataSource_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.