HomeSilverlight Silverlight 4.0: Query Parameters of Domai...
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.
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.
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:
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:
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.
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.
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).
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/.