You are on page 1of 8

WCF is the most suitable way to bind the data in Silverlight applications.

We can use LINQ


TO SQL classes to create the data layer mapped over SQL Server tables. Further by
creating Silverlight-enabled-WebService we can access above created LINQ classes to
access the data and fetching to Silverlight application.

First create a silverlight application.

Secondly,

Now that you have created the Silverlight 3 Application, we would add a LINQ to SQL file
that is *.dbml .

Thirdly,

To communicate with LINQ to SQL we need to have a service that client can call at any
time.

Add Silverlight enabled WCF Service to the Web Project.


In the Service.svc.cs add methods as per your requirement.

I have written two methods such as:

view plaincopy to clipboardprint?

1. #region [GetAllEmployees]
2.
3. [OperationContract]
4. List<vw_mst_EmployeeDetail> GetAllEmployees()
5. {
6. using (EmployeeDBDataContext context = new EmployeeDBDataContext())

7. {
8. var result = from emp in context.vw_mst_EmployeeDetails
9. select emp;
10.
11. return result.ToList();
12. }
13. }
14.
15. #endregion
16.
17. #region [GetEmployeesByFirstName]
18.
19. [OperationContract]
20. List<vw_mst_EmployeeDetail> GetEmployeesByFirstName(string firstName)
21. {
22. using (EmployeeDBDataContext context = new EmployeeDBDataContext())

23. {
24. var result = from emp in context.vw_mst_EmployeeDetails
25. where emp.FirstName.StartsWith(firstName)
26. select emp;
27.
28. return result.ToList();
29. }
30. }
31.
32. #endregion

Fourthly,

After writing the service build the project and get this service's reference in Silverlight 3
Project as shown in below figure.
#region [GetAllEmployees]
[OperationContract]
List<vw_mst_EmployeeDetail> GetAllEmployees()
{
using (EmployeeDBDataContext context = new EmployeeDBDataContext())
{
var result = from emp in context.vw_mst_EmployeeDetails
select emp;
return result.ToList();
}
}
#endregion
#region [GetEmployeesByFirstName]
[OperationContract]
List<vw_mst_EmployeeDetail> GetEmployeesByFirstName(string firstName)
{
using (EmployeeDBDataContext context = new EmployeeDBDataContext())
{
var result = from emp in context.vw_mst_EmployeeDetails
where emp.FirstName.StartsWith(firstName)
select emp;
return result.ToList();
}
}
#endregion

After you add the service it would create the ServiceReferences.ClientConfig file.

Now it's time to design our Silverlight UI in Blend, I have gone for a simple approach,
where simple buttons and textbox would help us getting the result which would be
displayed in DataGrid.
XAML for your reference is as follows -

<UserControl

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" xmlns:data="clr-
namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
x:Class="Mazeweb.Silverlight.MainPage"

d:DesignWidth="640" d:DesignHeight="480">

<Grid x:Name="LayoutRoot">

<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

<GradientStop Color="#FFF2F2F8" Offset="0.948"/>

<GradientStop Color="#FF5B99F2" Offset="0.665"/>

</LinearGradientBrush>

</Grid.Background>

<Grid.RowDefinitions>

<RowDefinition Height="0.106*"/>

<RowDefinition Height="0.056*"/>

<RowDefinition Height="0.838*"/>

</Grid.RowDefinitions>

<TextBlock HorizontalAlignment="Center" Text="LINQ TO SQL &amp; SILVERLIGHT


3"

TextWrapping="Wrap" FontSize="21.333" TextDecorations="Underline"


Margin="0,3,0,19" d:LayoutOverrides="Height"/>

<StackPanel Margin="0" Height="80" VerticalAlignment="Center"


Orientation="Horizontal"

HorizontalAlignment="Center" Grid.Row="1">

<Button x:Name="btnAll" Height="25" Content="Show All" Click="btnAll_Click"


VerticalAlignment="Center" Margin="0,0,0,8" Width="109" />

<Rectangle Height="25" Width="10"/>

<TextBox x:Name="txtName" Height="25" TextWrapping="Wrap" Width="164"


VerticalAlignment="Center"/>

<Rectangle Height="25" Width="10"/>

<Button x:Name="btnSubmit" Height="25" Content="Search"


Click="btnSubmit_Click" VerticalAlignment="Center" Margin="0,0,0,8" Width="109" />

</StackPanel>

<data:DataGrid x:Name="dgResults" Margin="8,10,8,89" Grid.Row="2"


IsReadOnly="True"

AutoGenerateColumns="False">
<data:DataGrid.Columns>

<data:DataGridTextColumn Binding="{Binding ID}"


Header="ID"></data:DataGridTextColumn>

<data:DataGridTextColumn Binding="{Binding FirstName}" Header="First


Name"></data:DataGridTextColumn>

<data:DataGridTextColumn Binding="{Binding LastName}" Header="Last


Name"></data:DataGridTextColumn>

<data:DataGridTextColumn Binding="{Binding UserCode}"


Header="EmailID"></data:DataGridTextColumn>

<data:DataGridTextColumn Binding="{Binding DesignationName}"


Header="Designation"></data:DataGridTextColumn>

<data:DataGridTextColumn Binding="{Binding BranchName}"


Header="Branch"></data:DataGridTextColumn>

</data:DataGrid.Columns>

</data:DataGrid>

</Grid>

</UserControl>

Finally,

Now we would write client side code to call the service and display the result.

Following code is for Show All Button Click and displaying the result.

private void btnSubmit_Click(object sender, RoutedEventArgs e)


{
ServiceClient clientService = new ServiceClient();
clientService.GetEmployeesByFirstNameCompleted += new
EventHandler<GetEmployeesByFirstNameCompletedEventArgs>(clientService_GetEmploy
eesByFirstNameCompleted);
clientService.GetEmployeesByFirstNameAsync(txtName.Text);
}

void clientService_GetEmployeesByFirstNameCompleted(object sender,


GetEmployeesByFirstNameCompletedEventArgs e)
{
dgResults.ItemsSource = e.Result;
}
private void btnAll_Click(object sender, RoutedEventArgs e)
{
ServiceClient clientService = new ServiceClient();
clientService.GetAllEmployeesCompleted += new
EventHandler<GetAllEmployeesCompletedEventArgs>(clientService_GetAllEmployeesCom
pleted);
clientService.GetAllEmployeesAsync();
}

void clientService_GetAllEmployeesCompleted(object sender,


GetAllEmployeesCompletedEventArgs e)
{
dgResults.ItemsSource = e.Result;
}

Now, Finally you can test your application successfully running.

You might also like