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

How to: Error Handling

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.

When you retrieve or modify data from a Silverlight client, you typically want to handle errors and take certain steps in response to the error. With WCF RIA Services, you can handle errors by providing a callback method for data operations and by checking for errors in that callback method.

Error Handling When Loading Data

When loading data from a query method, you can either handle the error or choose to ignore the error. Specifically, you choose from the following approaches:

  • Provide a callback method. In the callback method, handle the error and call the MarkErrorAsHandled method to indicate that the exception is not thrown.
  • In the Load method, set the throwOnError parameter to False when you call the Load method to indicate that you do not want an exception thrown for query errors.
  • Use the overload of the Load method which does not accept a parameter for a callback method or a boolean parameter. Any errors when running the query will result in an unhandled exception.

The following example shows how to load data from a query and specify a callback method that checks for errors from the load operation.

private SofiaCarRentalDomainContext dbContext = new SofiaCarRentalDomainContext();
public MainPage()
{
   InitializeComponent();
   LoadOperation<Customer> loadOperation = dbContext.Load( dbContext.GetCustomersQuery(), OnLoadCompleted, null );
   CustomerGrid.ItemsSource = loadOperation.Entities;
}
private void OnLoadCompleted( LoadOperation<Customer> loadOperation )
{
   if ( loadOperation.HasError )
   {
       MessageBox.Show( loadOperation.Error.Message );
       loadOperation.MarkErrorAsHandled();
   }
}

Error Handling When Submitting Data

When submitting data, you cannot choose to turn off the exceptions as you can with the method. Any errors when submitting data will result in an exception. Specifically, you choose from the following options:

  • Use the SubmitChanges method and provide a callback method as a parameter. In the callback method, handle the error and call the MarkErrorAsHandled method to indicate that the exception is not thrown.
  • Use the SubmitChanges method. Any errors when submitting the data will result in an unhandled exception.

The following example shows how to call the SubmitChanges method with a callback method for handling errors.

if ( this.dbContext.HasChanges == false )
   return;
dbContext.SubmitChanges( c =>
{
   if ( c.HasError )
   {
       MessageBox.Show( c.Error.Message );
       c.MarkErrorAsHandled();
       // Check all entities in error
       foreach ( var item in c.EntitiesInError )
       {
           foreach ( var validationError in item.ValidationErrors )
           {
           }
       }
   }
}, null );
If Me.dbContext.HasChanges = False Then
 Return
End If
  ' Check all entities in error
dbContext.SubmitChanges(Sub(c)
 If c.HasError Then
  MessageBox.Show(c.Error.Message)
  c.MarkErrorAsHandled()
  For Each item In c.EntitiesInError
   For Each validationError In item.ValidationErrors
   Next validationError
  Next item
 End If
End Sub, Nothing)