Walkthrough: Performing Create, Update, Delete 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.
When you add update, insert, or delete methods in a domain service, you can create an interface in a Silverlight client that enables the users to change the data.
In this walkthrough, you will learn how to create an interface that enables the user to add, edit and delete data, and how to save those changes in the databases.
This topic is based on the WCF RIA Services example solution from the Walkthrough: Creating a RIA Services Solution.
When the domain service includes methods for updating, inserting, and deleting, those methods are not generated in the domain context. Instead, you use the SubmitChanges method on the domain context and the proper operations on the domain service are called. No changes in the data source are made until you call SubmitChanges. You can cancel pending changes by calling the RejectChanges method.
To display, edit, add and delete data:
- In the client project, open MainPage.xaml.
- Add four Button controls - for Save, Add, Edit and Delete.
-
The following XAML shows a complete layout along with the existing DataGrid.
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="OA.SL.RIA.Demo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <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="btnEdit" Content="Edit" Margin="7,0" Click="btnEdit_Click" /> <Button x:Name="btnDelete" Content="Delete" Click="btnDelete_Click" /> </StackPanel> <sdk:DataGrid Name="CustomersDataGrid" Grid.Row="1" /> </Grid> </UserControl>
-
In the code-behind page for MainPage.xaml, add event handlers for the button click events:
using System.ServiceModel.DomainServices.Client; using System.Windows.Controls; using OA.SL.RIA.Demo.Web; using System.Windows; namespace OA.SL.RIA.Demo { public partial class MainPage : UserControl { private SofiaCarRentalDomainContext domainContext = new SofiaCarRentalDomainContext(); public MainPage() { InitializeComponent(); LoadOperation<Customer> loadOperation = domainContext.Load(domainContext.GetCustomersQuery()); loadOperation.Completed += (s, a) => { CustomersDataGrid.ItemsSource = domainContext.Customers; }; } private void btnSave_Click(object sender, RoutedEventArgs e) { } private void SubmitOperationCompleted(SubmitOperation submitOperation) { } private void btnAdd_Click(object sender, RoutedEventArgs e) { } private void btnEdit_Click(object sender, RoutedEventArgs e) { } private void btnDelete_Click(object sender, RoutedEventArgs e) { } } }
Imports System.ServiceModel.DomainServices.Client Imports OA.SL.RIA.Demo.Web Imports OA.SL.RIA.Demo.Web.OA.SL.RIA.Demo.Web Partial Public Class MainPage Inherits UserControl Private domainContext As New SofiaCarRentalDomainContext() Public Sub New() InitializeComponent() Dim _loadOperation As LoadOperation(Of Customer) = domainContext.Load(domainContext.GetCustomersQuery()) AddHandler _loadOperation.Completed, Sub(s, a) CustomersDataGrid.ItemsSource = domainContext.Customers End Sub Private Sub btnSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) End Sub Private Sub SubmitOperationCompleted(ByVal _submitOperation As SubmitOperation) End Sub Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) End Sub Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) End Sub Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) End Sub End Class
Note, that in the MainPage constructor all Customers are loaded async and set as ItemsSource for the DataGrid.
-
Add the following code in the btnSave_Click event handler. Its purpose is to submit changes to the DomainService, if such changes exist. Callback for the submit operation is passed. The submit operation callback allows you to check whether the operation has completed successfully or has failed. If the operation has failed, then you should inspect the Error property for more details.
private void btnSave_Click(object sender, RoutedEventArgs e) { if (this.domainContext.HasChanges == false) { return; } domainContext.SubmitChanges(this.SubmitOperationCompleted, null); } private void SubmitOperationCompleted(SubmitOperation submitOperation) { if (submitOperation.HasError) { // Something went wrong. MessageBox.Show(submitOperation.Error.Message); } else { // Everything is ok. MessageBox.Show("All changes are submitted successfully."); } }
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) If Me.domainContext.HasChanges = False Then Return End If domainContext.SubmitChanges(AddressOf Me.SubmitOperationCompleted, Nothing) End Sub Private Sub SubmitOperationCompleted(ByVal _submitOperation As SubmitOperation) If _submitOperation.HasError Then ' Something went wrong. MessageBox.Show(_submitOperation.Error.Message) Else ' Everything is ok. MessageBox.Show("All changes are submitted successfully.") End If End Sub
-
In the btnDelete_Click handler, add code for retrieving the currently selected Customer and for deleting it.
private void btnDelete_Click( object sender, RoutedEventArgs e ) { if ( this.CustomersDataGrid.SelectedItem == null ) return; Customer customerToDelete = this.CustomersDataGrid.SelectedItem as Customer; this.domainContext.Customers.Remove( customerToDelete ); }
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) If Me.CustomersDataGrid.SelectedItem Is Nothing Then Return End If Dim customerToDelete As Customer = TryCast(Me.CustomersDataGrid.SelectedItem, Customer) Me.domainContext.Customers.Remove(customerToDelete) End Sub
-
In the btnAdd_Click event handler, a new customer is created, initialized and passed to the DomainService.
private void btnAdd_Click( object sender, RoutedEventArgs e ) { Customer newCustomer = new Customer(); newCustomer.Address = "[Address]"; newCustomer.City = "[City]"; newCustomer.Country = "[Country]"; newCustomer.DrvLicNumber = "[DrvLicNumber]"; newCustomer.FullName = "[FullName]"; newCustomer.State = "[State]"; newCustomer.ZIPCode = "[ZipCode]"; domainContext.Customers.Add( newCustomer ); }
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Dim newCustomer As New Customer() newCustomer.Address = "[Address]" newCustomer.City = "[City]" newCustomer.Country = "[Country]" newCustomer.DrvLicNumber = "[DrvLicNumber]" newCustomer.FullName = "[FullName]" newCustomer.State = "[State]" newCustomer.ZIPCode = "[ZipCode]" domainContext.Customers.Add(newCustomer) End Sub
-
Finally, the btnEdit_Click event handler demonstrates you how to extract the currently selected Customer from the data grid and edit its properties.
private void btnEdit_Click(object sender, RoutedEventArgs e) { if (this.CustomersDataGrid.SelectedItem == null) { return; } Customer customer = this.CustomersDataGrid.SelectedItem as Customer; customer.Address = "NewAddress"; customer.City = "NewCity"; customer.Country = "NewCountry"; }
Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) If Me.CustomersDataGrid.SelectedItem Is Nothing Then Return End If Dim _customer As Customer = TryCast(Me.CustomersDataGrid.SelectedItem, Customer) _customer.Address = "NewAddress" _customer.City = "NewCity" _customer.Country = "NewCountry" End Sub