Silverlight 4.0: Query Parameters of DomainDataSource - A Second Glance

This article gives a complete idea of how to work with the query parameters of the “DomainDataSource” control available in Silverlight (using WCF RIA Services). It also discusses dynamic query parameters, which can be provided from code-behind.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
August 18, 2010
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

To make this article simple, I managed to create a simple Silverlight 4.0 application which consumes a WCF RIA Service created using the WCF RIA Service Library.  If you are not familiar with developing applications using the WCF RIA Service Library and Silverlight 4.0, check out a beginner’s article.

If you are unfamiliar with “DomainDataSource,” I strongly recommend that you go through my previous articles.

The entire source code for this article is available in the form of a free downloadable zip file.  The solution was developed using Microsoft Visual Studio 2010 Ultimate Edition with Microsoft Silverlight 4.0 on Windows 7 Ultimate edition.  I didn’t really test it in any other environment.  I request that you post in the discussion area if you have any problems in execution.

Linking Query Parameters of DomainDataSource to Controls in Silverlight 4.0

In my previous article, I introduced DomainDataSource in Silverlight 4.0.  It also explained how to work with query parameters of DomainDataSource. 

In this section, we will implement query parameters which would retrieve values from existing Silverlight controls on the form.

Let us start with an example:

<UserControl x:Class="SLBusinessAppWithRiaLib.DdsControlQueryParam"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.
DomainServices"
    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"
    xmlns:local="clr-namespace:BusinessLib.Web;assembly=BusinessLib"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Width="500" Height="500">

    <UserControl.Resources>
        <riaControls:DomainDataSource x:Name="EmpDomDataSource"
                                      LoadSize="15"
                                      QueryName="GetEmpsByDeptno"
                                      AutoLoad="True">
            <riaControls:DomainDataSource.DomainContext>
                <local:EmpMgrDomainSvc></local:EmpMgrDomainSvc>
            </riaControls:DomainDataSource.DomainContext>
            <riaControls:DomainDataSource.QueryParameters>
                <riaControls:Parameter ParameterName="deptno" Value="{Binding ElementName=txtDeptno, Path=Text}"/>
            </riaControls:DomainDataSource.QueryParameters>
        </riaControls:DomainDataSource>
    </UserControl.Resources>
   
    <Grid x:Name="LayoutRoot" Background="White">
     <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
     </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <sdk:Label x:Name="lblDeptno" Content="Enter Deptno:" Margin="8,8,8,8" />
            <TextBox x:Name="txtDeptno" Width="150" Height="25" Margin="8,8,8,8" />
        </StackPanel>
       
        <sdk:DataGrid Grid.Row="1"
            x:Name="dgEmp"
            Margin="8" MinHeight="200" IsReadOnly="True"
            AutoGenerateColumns="True"
            ItemsSource="{Binding Data, ElementName=EmpDomDataSource}" />

    </Grid>
</UserControl>

The design of the above form would look like the following screen shot:

The next section explains the previous code.

Linking Query Parameters of DomainDataSource to Controls in Silverlight 4.0, explained

This section explains the code provided in the previous section.

Let us start with the screen layout.

     <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
     </Grid.RowDefinitions>

I divided the entire screen into two rows. The second row fills the entire space left on the form. The content of the first row is defined as follows:

        <StackPanel Orientation="Horizontal">
            <sdk:Label x:Name="lblDeptno" Content="Enter Deptno:" Margin="8,8,8,8" />
            <TextBox x:Name="txtDeptno" Width="150" Height="25" Margin="8,8,8,8" />
        </StackPanel>

The above sits in the first row of the form.  That is, we will have a label (with “Enter Deptno:” as its content) and a TextBox sitting next to each other. Their placement is dictated by “StackPanel,” which is configured with the “Horizontal” orientation.  The content in the second row of the form is filled with the “DataGrid” as shown below:

        <sdk:DataGrid Grid.Row="1"
            x:Name="dgEmp"
            Margin="8" MinHeight="200" IsReadOnly="True"
            AutoGenerateColumns="True"
            ItemsSource="{Binding Data, ElementName=EmpDomDataSource}" />

The above “DataGrid” fills the entire space in the second row until it reaches the maximum width/height of the form. Furthermore, it is bound to “EmpDomDataSource,” which is defined as follows:

    <UserControl.Resources>
        <riaControls:DomainDataSource x:Name="EmpDomDataSource"
                                      LoadSize="15"
                                      QueryName="GetEmpsByDeptno"
                                      AutoLoad="True">
            <riaControls:DomainDataSource.DomainContext>
                <local:EmpMgrDomainSvc></local:EmpMgrDomainSvc>
            </riaControls:DomainDataSource.DomainContext>
            <riaControls:DomainDataSource.QueryParameters>
                <riaControls:Parameter ParameterName="deptno" Value="{Binding ElementName=txtDeptno, Path=Text}"/>
            </riaControls:DomainDataSource.QueryParameters>
        </riaControls:DomainDataSource>
    </UserControl.Resources>

The above markup explains the following:

• We define a DomainDataSource element named “EmpDomDataSource”

• It is bound to “EmpMgrDomainSvc” (nothing but context).

• It pulls data using “GetEmpsByDeptno” (an entity query of the domain service context).

• The query “GetEmpsByDeptno” accepts a parameter named “deptno.”

• The parameter value is directly taken from an existing control, “txtDeptno.”

Once we execute the above form and provide “30” as the department number, the output would look similar to the following screen shot:

Applying Query Parameters dynamically to a DomainDataSource using code-behind: design

In the previous section, we saw how to provide query parameters directly as part of the markup.  In this section, we try to achieve the same using code-behind.

Let us start with a new form as follows:

<UserControl x:Class="SLBusinessAppWithRiaLib.DdsControlQueryParamCodeBehind"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.
DomainServices"
    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"
    xmlns:local="clr-namespace:BusinessLib.Web;assembly=BusinessLib"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Width="500" Height="500">

    <UserControl.Resources>
        <riaControls:DomainDataSource x:Name="EmpDomDataSource">
            <riaControls:DomainDataSource.DomainContext>
                <local:EmpMgrDomainSvc></local:EmpMgrDomainSvc>
            </riaControls:DomainDataSource.DomainContext>
        </riaControls:DomainDataSource>
    </UserControl.Resources>
   
    <Grid x:Name="LayoutRoot" Background="White">
     <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
     </Grid.RowDefinitions>
       
        <StackPanel Orientation="Horizontal">
            <sdk:Label x:Name="lblDeptno" Content="Enter Deptno:" Margin="8,8,8,8" />
            <TextBox x:Name="txtDeptno" Width="150" Height="25" Margin="8,8,8,8" />
            <Button x:Name="btnShowEmp" Content="Show Employees" Height="25" Margin="8,8,8,8"></Button>
        </StackPanel>
       
        <sdk:DataGrid Grid.Row="1"
            x:Name="dgEmp"
            Margin="8" MinHeight="200" IsReadOnly="True"
            AutoGenerateColumns="True"
            ItemsSource="{Binding Data, ElementName=EmpDomDataSource}" />

    </Grid>
</UserControl>

You can observe from the above code that I removed the query parameters of the DomainDataSource control.  Also, I've added a “Button” (with the caption “Show Employees”). It sits next to the textbox which accepts the department number from user).

The design would look similar to the following screen shot:

My goal is to fill the data grid with a list of employees by using the DomainDataSource, when I clicked on the button. I would like to achieve this using dynamic query parameters. 

The code-behind part for the above form is provided in the next section.

Applying Query Parameters dynamically to a DomainDataSource using code-behind: code

This is code-behind for the form provided in the previous section.

Imports System.ServiceModel.DomainServices.Client
Imports System.Windows.Data

Partial Public Class DdsControlQueryParamCodeBehind
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub EmpDomDataSource_LoadedData(ByVal sender As Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles EmpDomDataSource.LoadedData
        If e.HasError Then
            System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK)
            e.MarkErrorAsHandled()
        End If
    End Sub

    Private Sub btnShowEmp_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnShowEmp.Click
        Dim oDdsrc As DomainDataSource = Me.Resources("EmpDomDataSource")

        Dim oCtxt As BusinessLib.Web.EmpMgrDomainSvc = oDdsrc.DomainContext
        oCtxt.EntityContainer.Clear()

        oDdsrc.QueryParameters.Clear()
        oDdsrc.QueryParameters.Add(New Parameter() With {.ParameterName = "deptno", .Value = Me.txtDeptno.Text})

        oDdsrc.QueryName = "GetEmpsByDeptno"
        oDdsrc.LoadSize = 15
        oDdsrc.Load()
    End Sub

End Class

Let us try to understand the above code part by part.

    Private Sub EmpDomDataSource_LoadedData(ByVal sender As Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles EmpDomDataSource.LoadedData
        If e.HasError Then
            System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK)
            e.MarkErrorAsHandled()
        End If
    End Sub

The above code gets executed if the form runs into any errors. It simply displays a message box with the error message. Now, we will examine the code provided for “btnShowEmp.”

        Dim oDdsrc As DomainDataSource = Me.Resources("EmpDomDataSource")

The above line gets hold of the DomainDataSource declared in the markup. The reference to that element gets stored in “oDdsrc.”

        Dim oCtxt As BusinessLib.Web.EmpMgrDomainSvc = oDdsrc.DomainContext

Once we get hold of the DomainDatasource, we need to work with the Domain Context.  We can easily get hold of the domain context available in DomainDatasource by using the property “DomainContext” (as shown above).

        oCtxt.EntityContainer.Clear()

Once we get hold of the domain context available in DomainDataSource, we clear all existing entities using the above statement.

        oDdsrc.QueryParameters.Clear()
        oDdsrc.QueryParameters.Add(New Parameter() With {.ParameterName = "deptno", .Value = Me.txtDeptno.Text})

Adding a new query parameter to a DomainDataSource can be easily achieved with “QueryParameters” collection. Before we add any new query parameters, I removed the existing ones (just wanted to demonstrate on how we can remove existing parameters as well).

        oDdsrc.QueryName = "GetEmpsByDeptno"
        oDdsrc.LoadSize = 15
        oDdsrc.Load()

Finally, DomainDataSource refreshes data in its context with the above statements.

Once you execute the above form, the output would look similar to the following:


My upcoming articles will focus on sorting, grouping, filtering and paginating using the “DomainDataSource” element.  I hope you enjoyed this article; any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com/.

blog comments powered by Disqus
SILVERLIGHT ARTICLES

- With Silverlight Gone, Whither SharePoint?
- Silverlight in the News
- Silverlight Has a Bright Future
- Windows 8 Effects on .Net and Silverlight De...
- Microsoft`s SkyDrive Abandons Silverlight
- Silverlight Developers Unhappy with Windows ...
- Best Silverlight Examples
- How to install Silverlight for Windows Phone
- Microsoft Reveals Silverlight 5 Features
- Silverlight News: SRS and Microsoft to Bring...
- Silverlight 4.0: Paging Through Data using D...
- Silverlight 4.0: Navigating Data Using Domai...
- Silverlight 4.0: Filtering Data Using Domain...
- Silverlight 4.0: Sorting and Grouping Data w...
- Silverlight 4.0: Query Parameters of DomainD...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials