Share via


Creating the .NET Framework Client Application (ADO.NET Data Services Quickstart)

Note

This topic describes new functionality in ADO.NET Data Services that is available as an update to the .NET Framework version 3.5 Service Pack 1. You can download and install the update from the Microsoft Download Center.

This is the final task of the ADO.NET Data Services quickstart.

Note

To complete this task, you must install an update to the .NET Framework version 3.5 Service Pack 1. You can download and install the update from the Microsoft Download Center

In this task, you will add a console application to the solution, add a reference to the data service into this new client application, and access the data service from the client application by using the generated client data service classes and client libraries.

Note

Because this example binds data service data to WPF controls, you cannot simply create the client data service classes by using the Add Service Reference dialog. This version of ADO.NET Data Services cannot generate the code needed for binding when you use the Add Web Reference dialog.

To create the client application by using Visual Studio

  1. In Solution Explorer, right-click the solution, click Add, and then click New Project.

  2. In Project types, click Windows, and then select WPF Application in the Templates pane.

  3. Enter NorthwindClient for the project name, and then click OK.

  4. Open the file Window1.xaml and replace the XAML code with the following code:

    <Window x:Class="Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="Northwind Orders" Height="335" Width="425" 
            Name="OrdersWindow" Loaded="Window1_Loaded">
        <Grid Name="orderItemsGrid">
            <ComboBox DisplayMemberPath="OrderID" ItemsSource="{Binding}"
                      IsSynchronizedWithCurrentItem="true" 
                      Height="23" Margin="92,12,198,0" Name="comboBoxOrder" VerticalAlignment="Top"/>
            <ListView ItemsSource="{Binding Path=Order_Details}" Name="orderItemsDataGrid" Margin="34,46,34,50">
                <ListView.View>
                    <GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Line Items">
                        <GridViewColumn DisplayMemberBinding="{Binding ProductID}" 
                                        Header="Product" Width="80" />
                        <GridViewColumn DisplayMemberBinding="{Binding Quantity}" 
                                        Header="Quantity" Width="80" />
                        <GridViewColumn DisplayMemberBinding="{Binding UnitPrice}" 
                                        Header="Price" Width="80"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Discount}" 
                                        Header="Discount" Width="80"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top" 
                   HorizontalAlignment="Left" Width="65">Order:</Label>
            <StackPanel Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right" 
                        Height="40" Margin="0,257,22,0">
                <Button Height="23" Margin="0,0,12,12" 
                    Name="buttonClose" VerticalAlignment="Bottom" Width="75" 
                        Click="buttonClose_Click">Close</Button>
            </StackPanel>
        </Grid>
    </Window>
    

To generate client data service classes for the Northwind service

  1. At the command prompt, execute one of the following commands, depending on the language of your project:

    C#

    "%windir%\Microsoft.NET\Framework\v3.5\DataSvcUtil.exe" /dataservicecollection /version:2.0 /language:CSharp /out:Northwind.cs /uri:https://localhost:12345/Northwind.svc
    

    Visual Basic

    "%windir%\Microsoft.NET\Framework\v3.5\DataSvcUtil.exe" /dataservicecollection /version:2.0 /language:VB /out:Northwind.vb /uri:https://localhost:12345/Northwind.svc
    

    This generates an output file in the desired language that contains the client data service classes.

  2. In Visual Studio, on the Project menu, click Add Existing Item, browse to the directory location in which the previous commands were executed, select the newly generated file, and then click OK.

  3. This adds the generated client data service classes to the project.

To access data service data in the WPF application

  1. In Solution Explorer under NorthwindClient, right-click the project and click Add Reference.

  2. In the Add Reference dialog box, click the .NET tab, select the System.Data.Services.Client.dll assembly, and then click OK.

  3. In Solution Explorer under NorthwindClient, open the code page for the Window1.xaml file, and add the following using statement (Imports in Visual Basic).

    Imports System.Data.Services.Client
    Imports NorthwindClient.NorthwindModel
    Imports NorthwindClient
    
    using System.Data.Services.Client;
    using NorthwindModel;
    
  4. Insert the following code that queries that data service and binds the result to a DataServiceCollection into the Window1 class:

    Note

    You must replace the host name localhost:12345 with the server and port that is hosting your instance of the Northwind data service.

    Private context As NorthwindEntities
    Private customerId As String = "ALFKI"
    
    ' Replace the host server and port number with the values 
    ' for the test server hosting your Northwind data service instance.
    Private svcUri As Uri = New Uri("https://localhost:12345/Northwind.svc")
    
    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Try
            ' Instantiate the DataServiceContext.
            context = New NorthwindEntities(svcUri)
    
            ' Define a LINQ query that returns Orders and 
            ' Order_Details for a specific customer.
            Dim ordersQuery = From o In context.Orders.Expand("Order_Details") _
                                  Where o.Customers.CustomerID = customerId _
                                  Select o
    
            ' Create an DataServiceCollection(Of T) based on 
            ' execution of the LINQ query for Orders.
            Dim customerOrders As DataServiceCollection(Of Orders) = New  _
                DataServiceCollection(Of Orders)(ordersQuery)
    
            ' Make the DataServiceCollection<T> the binding source for the Grid.
            Me.orderItemsGrid.DataContext = customerOrders
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
    Private Sub buttonClose_Click(ByVal sender As Object, ByVal a As RoutedEventArgs)
        Me.Close()
    End Sub
    
    private NorthwindEntities context;
    private string customerId = "ALFKI";
    
    // Replace the host server and port number with the values 
    // for the test server hosting your Northwind data service instance.
    private Uri svcUri = new Uri("https://localhost:12345/Northwind.svc");
    
    private void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            // Instantiate the DataServiceContext.
            context = new NorthwindEntities(svcUri);
    
            // Define a LINQ query that returns Orders and 
            // Order_Details for a specific customer.
            var ordersQuery = from o in context.Orders.Expand("Order_Details")
                              where o.Customers.CustomerID == customerId
                              select o;
    
            // Create an DataServiceCollection<T> based on 
            // execution of the LINQ query for Orders.
            DataServiceCollection<Orders> customerOrders = new
                DataServiceCollection<Orders>(ordersQuery);
    
            // Make the DataServiceCollection<T> the binding source for the Grid.
            this.orderItemsGrid.DataContext = customerOrders;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
    private void buttonClose_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
    

To build and run the NorthwindClient application

  1. In Solution Explorer, right-click the NorthwindClient project and select Set as startup project.

  2. Press F5 to start debugging the application.

    This builds the solution and starts the client application. Data is requested from the service and displayed in the console.

Next Steps

You have successfully created the client application that accesses the sample Northwind data service. You have also completed the ADO.NET Data Services quickstart. For more information about accessing ADO.NET Data Services from a .NET Framework application, see Using a Data Service in a .NET Framework Application (ADO.NET Data Services).

See Also

Other Resources

Getting Started with ADO.NET Data Services

ADO.NET Data Services Resources