Professional Documents
Culture Documents
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.
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">
</LinearGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="0.106*"/>
<RowDefinition Height="0.056*"/>
<RowDefinition Height="0.838*"/>
</Grid.RowDefinitions>
HorizontalAlignment="Center" Grid.Row="1">
</StackPanel>
AutoGenerateColumns="False">
<data:DataGrid.Columns>
</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.