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

Walkthrough: Performing Create, Update, Delete

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.

In this walkthrough, you will learn how to create an interface that enables the user to add, edit and delete data, and save those changes in the databases.

This topic is based on the WCF Data Services example solution from the Walkthrough: Creating a WCF Data Services Solution.

The methods that handle the events which are raised by the controls call the AddObject, UpdateObject and DeleteObjects methods to set the state of the object in the DataManager. The BeginSaveChanges and EndSaveChanges methods on the DataManager are called asynchronously to save changes back.

To display, edit, add and delete data:

  1. In the client project, open MainPage.xaml.
  2. Add three Button controls - for Save, Add and Delete.
  3. The following XAML shows a complete layout along with the existing DataGrid. Note that you should also attach to the DataGrid's CellEditEnded event.

    <UserControl x:Class="OA.SL.WcfData.Demo.MainPage"
       xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
       <Grid x:Name="LayoutRoot"
             Background="White">
           <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition />
           </Grid.RowDefinitions>
           <StackPanel Orientation="Horizontal"
                       Margin="0,3">
               <Button x:Name="btnSave"
                       Content="Save"
                       Click="btnSave_Click" />
               <Button x:Name="btnAdd"
                       Content="Add"
                       Margin="7,0"
                       Click="btnAdd_Click" />
               <Button x:Name="btnDelete"
                       Content="Delete"
                       Click="btnDelete_Click" />
           </StackPanel>
           <sdk:DataGrid Name="CustomerGrid"
                         Grid.Row="1"
                         CellEditEnded="CustomerGrid_CellEditEnded" />
       </Grid>
    </UserControl>
    
  4. In the code-behind page for MainPage.xaml, add event handlers for the button click events:

    using System;
    using System.Collections.ObjectModel;
    using System.Data.Services.Client;
    using System.Windows.Controls;
    using OA.SL.WcfData.Demo.SofiaCarRentalWcfDataService;
    namespace OA.SL.WcfData.Demo
    {
       public partial class MainPage : UserControl
       {
           private SofiaCarRentalWcfDataService.SofiaCarRentalDbContext dataManager = 
               new SofiaCarRentalWcfDataService.SofiaCarRentalDbContext( 
                   new Uri( "SofiaCarRentalWCFDataService.svc", UriKind.Relative ) );
           private ObservableCollection<Customer> dataSource = new ObservableCollection<Customer>();
           public MainPage()
           {
               InitializeComponent();
               this.CustomerGrid.ItemsSource = dataSource;
               DataServiceQuery<Customer> query = dataManager.Customers;
               query.BeginExecute(
                   s =>
                   {
                       DataServiceQuery<Customer> state = s.AsyncState as DataServiceQuery<Customer>;
                       foreach ( var entity in state.EndExecute( s ) )
                           dataSource.Add( entity );
                   }, query );
           }
           private void btnSave_Click( object sender, System.Windows.RoutedEventArgs e )
           {
           }
           private void btnAdd_Click( object sender, System.Windows.RoutedEventArgs e )
           {
           }
           private void btnDelete_Click( object sender, System.Windows.RoutedEventArgs e )
           {
           }
           private void CustomerGrid_CellEditEnded( object sender, DataGridCellEditEndedEventArgs e )
           {
           }
       }
    }
    
    Imports System.Data.Services.Client
    Imports OA.SL.WcfData.Demo.SofiaCarRentalWCFDataService
    Imports System.Collections.ObjectModel
    Partial Public Class MainPage
        Inherits UserControl
       Private dataManager As New SofiaCarRentalWcfDataService.SofiaCarRentalDbContext(New _
           Uri("SofiaCarRentalWCFDataService.svc", UriKind.Relative))
        Private dataSource As New ObservableCollection(Of Customer)()
        Public Sub New()
            InitializeComponent()
            Me.CustomerGrid.ItemsSource = dataSource
            Dim query As DataServiceQuery(Of Customer) = dataManager.Customers
            query.BeginExecute(Sub(s)
               Dim state As DataServiceQuery(Of Customer) = 
                   TryCast(s.AsyncState, DataServiceQuery(Of Customer))
               For Each entity In state.EndExecute(s)
                   dataSource.Add(entity)
               Next entity
           End Sub, query)
        End Sub
        Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        End Sub
        Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        End Sub
        Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        End Sub
        Private Sub CustomerGrid_CellEditEnded(ByVal sender As Object, ByVal e As DataGridCellEditEndedEventArgs)
        End Sub
    End Class
    

    Note, that in the MainPage constructor all Customers are loaded async and set as ItemsSource for the DataGrid.

  5. Add the following code in the btnSave_Click event handler. Its purpose is to submit changes to the WCF Data Service, if such exist.

    private void btnSave_Click( object sender, System.Windows.RoutedEventArgs e )
    {
       this.dataManager.BeginSaveChanges( SaveChangesOptions.ContinueOnError,
           c =>
           {
               ( c.AsyncState as SofiaCarRentalDbContext ).EndSaveChanges( c );
           }, this.dataManager );
    }
    
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
     Me.dataManager.BeginSaveChanges(SaveChangesOptions.ContinueOnError, Sub(c) _ 
         TryCast(c.AsyncState, SofiaCarRentalDbContext) .EndSaveChanges(c), Me.dataManager)
    End Sub
    
  6. In the btnDelete_Click handler add code for retrieving the currently selected Customer and deleting it.

    private void btnDelete_Click( object sender, System.Windows.RoutedEventArgs e )
    {
       Customer customerToDelete = this.CustomerGrid.SelectedItem as Customer;
       this.dataSource.Remove( customerToDelete );
       this.dataManager.DeleteObject( customerToDelete );
    }
    
    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
     Dim customerToDelete As Customer = TryCast(Me.CustomerGrid.SelectedItem, Customer)
     Me.dataSource.Remove(customerToDelete)
     Me.dataManager.DeleteObject(customerToDelete)
    End Sub
    
  7. In the btnAdd_Click event handler a new customer is created, initialized and passed to the service.

    private void btnAdd_Click( object sender, System.Windows.RoutedEventArgs e )
    {
       Customer newCustomer = new Customer();
       newCustomer.Address = "[Address]";
       newCustomer.City = "[City]";
       newCustomer.Country = "[Country]";
       newCustomer.DrvLicNumber = "[DrvLicNumber]";
       newCustomer.FullName = "[FullName]";
       newCustomer.State = "[State]";
       this.dataSource.Add( newCustomer );
       this.dataManager.AddObject( "Customers", newCustomer );
    }
    
    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
     Dim newCustomer As New Customer()
     newCustomer.Address = "[Address]"
     newCustomer.City = "[City]"
     newCustomer.Country = "[Country]"
     newCustomer.DrvLicNumber = "[DrvLicNumber]"
     newCustomer.FullName = "[FullName]"
     newCustomer.State = "[State]"
     Me.dataSource.Add(newCustomer)
     Me.dataManager.AddObject("Customers", newCustomer)
    End Sub
    
  8. Finally add the following code in the CustomerGrid_CellEditEnded event handler. If any customer is updated in the DataGrid, then the UpdateObject method should be called.

    private void CustomerGrid_CellEditEnded( object sender, DataGridCellEditEndedEventArgs e )
    {
       if ( e.EditAction == DataGridEditAction.Commit )
           this.dataManager.UpdateObject( e.Row.DataContext as Customer );
    }
    
    Private Sub CustomerGrid_CellEditEnded(ByVal sender As Object, ByVal e As DataGridCellEditEndedEventArgs)
     If e.EditAction = DataGridEditAction.Commit Then
      Me.dataManager.UpdateObject(TryCast(e.Row.DataContext, Customer))
     End If
    End Sub