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

Adding Service Reference and Loading Data

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 task, you will use the already created empty Silverlight application in the Getting Started topic. In this application you will add a data service reference to the SofiaCarRental data service, which was created in the Creating WCF Data Service task.

Adding Service Reference

To add a WCF Data Service reference to the project:

  1. Right-click the SilverlightClient project, click Add Service Reference, and then click Discover. This displays the SofiaCarRentalWCFDataService that you have created in the previous task.
  2. In the Namespace text box, type SofiaCarRentalWcfDataService, and then click OK.

    This adds to the project a new code file that contains the data classes which are used to access and interact with data service resources as objects. The data classes are defined in the default namespace of the client application.

Defining User Interface

To define the Silverlight client application user interface:

  1. Open MainPage.xaml.
  2. From the Toolbox, drag a DataGrid control to within the Grid element in XAML view. Name the DataGrid CustomersGrid as shown in the following XAML.

    <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">
           <data:DataGrid Name="CustomersGrid" />
       </Grid>
    </UserControl>
    
  3. Open the code-behind for MainPage.xaml.

  4. Add code to instantiate the SofiaCarRentalDomainContext, retrieve customers by calling the GetCustomersQuery method, and bind the results to the DataGrid, as shown in the following code.

    using System;
    using System.Collections.ObjectModel;
    using System.Data.Services.Client;
    using System.Windows.Controls;
    using SilverlightOpenAccessIntegration.SofiaCarRentalWcfDataService;
    namespace SilverlightOpenAccessIntegration
    {
       public partial class MainPage : UserControl{
           private SofiaCarRentalDbContext dataManager =
               new SofiaCarRentalDbContext( new Uri( "SofiaCarRentalWCFDataService.svc", UriKind.Relative ) );
           public MainPage()
           {
               InitializeComponent();
               DataServiceQuery<Customer> query = dataManager.Customers;
               query.BeginExecute(
                   s =>
                   {
                       DataServiceQuery<Customer> state = s.AsyncState as DataServiceQuery<Customer>;
                       ObservableCollection<Customer> data = new ObservableCollection<Customer>();
                       foreach ( var entity in state.EndExecute( s ) )
                           data.Add( entity );
                       this.CustomersGrid.ItemsSource = data;
                   }, query );
           }
       }
    }
    
    Imports System.Data.Services.Client
    Imports System.Collections.ObjectModel
    Imports SilverlightOpenAccessIntegration.SofiaCarRentalWcfDataService
    Partial Public Class MainPage
        Inherits UserControl
        Private dataManager As New SofiaCarRentalWcfDataService.
             SofiaCarRentalDbContext(New Uri("SofiaCarRentalWCFDataService.svc", UriKind.Relative))
        Public Sub New()
            InitializeComponent()
            Dim query As DataServiceQuery(Of Customer) = dataManager.Customers
            query.BeginExecute(Sub(s)
                                   Dim state As DataServiceQuery(Of Customer) = _
                                       TryCast(s.AsyncState, DataServiceQuery(Of Customer))
                                   Dim data As New ObservableCollection(Of Customer)()
                                   For Each entity In state.EndExecute(s)
                                       data.Add(entity)
                                   Next entity
                                   Me.CustomersGrid.ItemsSource = data
                               End Sub, query)
        End Sub
    End Class
    
  5. Run the application. You should see a data grid that is similar to the following.