ComponentOne DataGrid for WPF and Silverlight
In This Topic
    Step 3 of 3: Connecting the Web Service
    In This Topic

    In the previous step you created a Web Service and added a database to your project. In this step you'll continue by linking the Web Service to your application. Note that this step requires ComponentOne Data for Silverlight.

    To set up your project, complete the following steps:

    1.        In the Solution Explorer, expand the project's node, right-click the project name (for example ComponentOneDataGrid) and select Add Reference from the context menu.

    1.        In the Add Reference dialog box, add a reference to the C1.Silverlight.Data assembly and click OK.

    2.        In the Solution Explorer, right-click the project name and select Add Service Reference from the context menu.

    3.        In the Add Service Reference dialog box click the Discover button. The DataService.asmx file will appear in the list of Services.

    4.        In the Namespace text box, change the default value to "DataService" and click the OK button to save your settings and close the dialog box.

    5.        In the Solution Explorer, expand the MainPage.xaml node and double-click the MainPage.xaml.cs or MainPage.xaml.vb file to open it in the Code Editor.

    6.        Add the following import statements at the top of the file:

    ·         Visual Basic

    Imports System.IO

    Imports C1.Silverlight.Data

    Imports ComponentOneDataGrid.DataService ' ComponentOneDataGrid is the project's namespace, change this if the name of your project is different.

    ·         C#

    using System.IO;

    using C1.Silverlight.Data;

    using ComponentOneDataGrid.DataService; // ComponentOneDataGrid is the project's namespace, change this if the name of your project is different.

    7.        Add LoadData(); to the MainPage constructor so it appears like the following:

    ·         Visual Basic

    Public Sub New()

        InitializeComponent()

        LoadData()

    End Sub

    ·         C#

    public MainPage()

    {

        InitializeComponent();

        LoadData();

    }

    8.        Add the LoadData and svc_GetDataCompleted methods to retrieve data from the Web Service:

    ·         Visual Basic

    Private _ds As DataSet = Nothing

    Private Sub LoadData()

       ' Invoke Web Service

       Dim svc = GetDataService()

       AddHandler svc.GetDataCompleted, AddressOf svc_GetDataCompleted

       'svc.GetDataAsync("Categories,Products,Employees");

       svc.GetDataAsync("Employees")

    End Sub

    Private Sub svc_GetDataCompleted(sender As Object, e As GetDataCompletedEventArgs)

       ' Handle errors

       If e.[Error] IsNot Nothing Then

             _tbStatus.Text = "Error downloading data..."

             Return

       End If

       ' Parse data stream from server (DataSet as XML)

       _tbStatus.Text = String.Format("Got data, {0:n0} kBytes", e.Result.Length / 1024)

       Dim ms = New MemoryStream(e.Result)

       _ds = New DataSet()

       _ds.ReadXml(ms)

       ' Bind control to the data

       BindData()

    End Sub

    ·         C#

    DataSet _ds = null;

    void LoadData()

    {

        // Invoke Web Service

        var svc = GetDataService();

        svc.GetDataCompleted += svc_GetDataCompleted;

        //svc.GetDataAsync("Categories,Products,Employees");  

        svc.GetDataAsync("Employees");

    }

    void svc_GetDataCompleted(object sender, GetDataCompletedEventArgs e)

    {

        // Handle errors

        if (e.Error != null)

        {

            _tbStatus.Text = "Error downloading data...";

            return;

        }

        // Parse data stream from server (DataSet as XML)

        _tbStatus.Text = string.Format("Got data, {0:n0} kBytes", e.Result.Length / 1024);

        var ms = new MemoryStream(e.Result);

        _ds = new DataSet();

        _ds.ReadXml(ms);

        // Bind control to the data

        BindData();

    }

    9.        Implement the GetDataService() method by adding the following code:

    ·         Visual Basic

    ' Get data service relative to current host/domain

    Private Function GetDataService() As DataServiceSoapClient

       ' Increase buffer size

       Dim binding = New System.ServiceModel.BasicHttpBinding()

       binding.MaxReceivedMessageSize = 2147483647

       ' int.MaxValue

       binding.MaxBufferSize = 2147483647

       ' int.MaxValue

       ' Get absolute service address

       Dim uri As Uri = C1.Silverlight.Extensions.GetAbsoluteUri("DataService.asmx")

       Dim address = New System.ServiceModel.EndpointAddress(uri)

       ' Return new service client

       Return New DataServiceSoapClient(binding, address)

    End Function

    ·         C#

    // Get data service relative to current host/domain

    DataServiceSoapClient GetDataService()

    {  

        // Increase buffer size

        var binding = new System.ServiceModel.BasicHttpBinding();

        binding.MaxReceivedMessageSize = 2147483647;

        // int.MaxValue

        binding.MaxBufferSize = 2147483647;

        // int.MaxValue

        // Get absolute service address

        Uri uri = C1.Silverlight.Extensions.GetAbsoluteUri("DataService.asmx");

        var address = new System.ServiceModel.EndpointAddress(uri);

        // Return new service client

        return new DataServiceSoapClient(binding, address);

    }

    10.     Implement the BindData() method by adding the following code:

    ·         Visual Basic

    Private Sub BindData()

       ' Get the tables

       Dim dtEmployees As DataTable = _ds.Tables("Employees")

       ' Populate categories grid

       _c1DataGrid.ItemsSource = dtEmployees.DefaultView

    End Sub

    ·         C#

    void BindData()

    {  

        // Get the tables

        DataTable dtEmployees = _ds.Tables["Employees"];             

        // Populate categories grid

        _c1DataGrid.ItemsSource = dtEmployees.DefaultView;

    }

    11.     Run your application and observe that the grid appears bound to the Employees table of the Northwind database:

     What You've Accomplished

    Congratulations, you've completed this tutorial! In this tutorial you created a new Silverlight project, added an Access database, and created a Web Service to bind the C1DataGrid control.