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

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.

  1. In the client project, open MainPage.xaml.
  2. Add three Button controls - for 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 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
                x:Class="SilverlightOpenAccessIntegration.MainPage"
                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"
                mc:Ignorable="d"
                d:DesignHeight="300"
                d:DesignWidth="400">
       <Grid x:Name="LayoutRoot"
             Background="White">
           <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition />
           </Grid.RowDefinitions>
           <StackPanel Orientation="Horizontal"
                       Margin="0,3">
    
               <Button x:Name="btnAdd"
                       Content="Add"
                       Margin="7,0"
                       Click="btnAdd_Click" />
               <Button x:Name="btnDelete"
                       Content="Delete"
                       Click="btnDelete_Click" />
           </StackPanel>
           <data:DataGrid Name="CustomersGrid"
                          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.Windows.Controls;
    using SilverlightOpenAccessIntegration.SofiaCarRentalWcfPlainService;
    namespace SilverlightOpenAccessIntegration
    {
       public partial class MainPage : UserControl
       {
           private SofiaCarRentalWCFEndPointServiceClient serviceClient = 
              new SofiaCarRentalWCFEndPointServiceClient();
    
           public MainPage()
           {
               InitializeComponent();
               serviceClient.ReadCustomersCompleted += serviceClient_ReadCustomersCompleted;
               serviceClient.ReadCustomersAsync();
           }
    
           void serviceClient_ReadCustomersCompleted( object sender, ReadCustomersCompletedEventArgs e )
           {
               if(e.Error == null )
               {
                   this.CustomersGrid.ItemsSource = e.Result;
               }
           }
    
           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 SilverlightOpenAccessIntegration.SofiaCarRentalWcfPlainService
    Partial Public Class MainPage
        Inherits UserControl
        Private serviceClient As New SofiaCarRentalWCFEndPointServiceClient()
        Public Sub New()
            InitializeComponent()
            AddHandler serviceClient.ReadCustomersCompleted, AddressOf serviceClient_ReadCustomersCompleted
            serviceClient.ReadCustomersAsync()
        End Sub
    
        Private Sub serviceClient_ReadCustomersCompleted(ByVal sender As Object, ByVal e As ReadCustomersCompletedEventArgs)
            If e.Error Is Nothing Then
                Me.CustomersGrid.ItemsSource = e.Result
            End If
        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. 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 )
    {
       CustomerDto customerToDelete = this.CustomersGrid.SelectedItem as CustomerDto;
       this.serviceClient.DeleteCustomerAsync( customerToDelete );
    }
    
    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
     Dim customerToDelete As CustomerDto = TryCast(Me.CustomersGrid.SelectedItem, CustomerDto)
     Me.serviceClient.DeleteCustomerAsync(customerToDelete)
    End Sub
    
  6. 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 )
    {
       CustomerDto newCustomer = new CustomerDto();
       newCustomer.Address = "[Address]";
       newCustomer.City = "[City]";
       newCustomer.Country = "[Country]";
       newCustomer.DrvLicNumber = "[DrvLicNumber]";
       newCustomer.FullName = "[FullName]";
       newCustomer.State = "[State]";
       this.serviceClient.CreateCustomerAsync( newCustomer );
    }
    
    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
     Dim newCustomer As New CustomerDto()
     newCustomer.Address = "[Address]"
     newCustomer.City = "[City]"
     newCustomer.Country = "[Country]"
     newCustomer.DrvLicNumber = "[DrvLicNumber]"
     newCustomer.FullName = "[FullName]"
     newCustomer.State = "[State]"
     Me.serviceClient.CreateCustomerAsync(newCustomer)
    End Sub
    
  7. Finally add the following code in the CustomerGrid_CellEditEnded event handler.

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