How to: Handle Data Events

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

You can customize your application by writing code that runs when certain data events happen. For example, you can write code that will run when rows of data in a table are created, accessed, modified, or deleted. You can also write code to verify that a user has permission to modify the data source.

The events you can handle can be grouped into six categories:

  • General methods that are run when data is created, deleted, or modified.

  • Access control methods that are run when data is created, deleted, or modified.

  • Query methods that are run when a data source is queried.

  • Data source methods that are run when a data source is saved to.

  • Property methods that are run when a property is modified.

A description of these methods appears in the tables at the end of this topic.

To handle a data-related event

  1. In Solution Explorer, double-click an entity or table.

    The entity or table opens in the Data Designer.

  2. On the command bar in the Data Designer, click the arrow next to the Write Code button, and then select a method that you want to override. The methods that can be handled by your application appear in the tables below.

    System_CAPS_ICON_note.jpg Note

    The Property Methods will only appear in the Write Code dropdown list if a property is selected in the Data Designer.

    The Code Editor opens.

  3. Place your cursor in the method that was just created and type the code that you want to run when the event occurs.

The following tables list the data events that can be handled by your application:

General MethodsDescription
<TableName>_AllowSaveWithErrorsIndicates whether the item should be saved if the item has validation errors. Save will abort by default if the item has validation errors. Runs on the calling tier.
<TableName>_CreatedCalled after the item is created. Runs on the tier where the item was created.
<TableSetName>_DeletedCalled just after the item has been deleted from the data store. Runs on the server.
<TableSetName>_DeletingCalled just before the item is deleted from the data store. Runs on the server.
<TableSetName>_InsertedCalled just after the item has been inserted into the data store. Runs on the server.
<TableSetName>_InsertingCalled just before the item is inserted into the data store. Runs on the server.
<TableSetName>_UpdatedCalled just after the item has been updated in the data store. Runs on the server.
<TableSetName>_UpdatingCalled just before the item is updated in the data store. Runs on the server.
<TableSetName>_ValidateCalled when an item is validated on the server.
Access Control MethodsDescription
SaveChanges_CanExecuteCalled prior to saving changes in the data source to check the permission level of the current user. Runs on the server.
<TableSetName>_CanDeleteCalled prior to deleting an item to check permission level of the current user. Runs on the server.
<TableSetName>_CanInsertCalled prior to inserting an item to check the permission level of the current user. Runs on the server.
<TableSetName>_CanReadCalled prior to reading an item to check the permission level of the current user. Runs on the server.
<TableSetName>_CanUpdateCalled prior to updating an item to check the permission level of the current user. Runs on the server.
Query MethodsDescription
Query_ExecuteFailedCalled after the query fails to execute. Runs on the server.
Query_ExecutedCalled just after executing the query. This method is not called if the query fails to execute. Runs on the server.
Query_ExecutingCalled just before executing the query. Runs on the server.
<TableSetName>_FilterCalled before the query runs, allowing for additional query customization. Runs on the server.
Data Source MethodsDescription
SaveChanges_ExecuteFailedCalled just after failing to save on the data source. Runs on the server.
SaveChanges_ExecutedCalled just after successfully saving changes in the data source. Runs on the server.
SaveChanges_ExecutingCalled just before saving changes in the data source. Runs on the server.
Property Methods
<PropertyName>_ChangedCalled just after the property value for an item has changed. Runs on the tier where the property was changed.
<PropertyName>_IsReadOnlyReturns whether the property is read-only. Runs on the tier where the property is accessed.
<PropertyName>_ValidateCalled when the property is validated. Runs on the tier where the property is validated.

Data: The Information Behind Your Application
How to: Handle Silverlight Screen Events
How to: Handle Query Events

Show: