Introduction:
In this article,i am going to demonstrate how to create a data driven applications using
silverlight linq and wcf.
Main:
Open Visual Studio 2010 with the Silverlight 4 tools installed. Once inside the Integrated Development Environment (IDE), Go to File | New | Project…. In the New Project dialog that appears, select the Silverlight node under Visual C# and select Silverlight Application. Name the application as SilverlightEmployeeBrowser and click the OK button.

In the dialog that appears as shown in the next screenshot, select ASP.NET Web Application Project as the type of web project that will be used to host the Silverlight application. Also, make sure that Silverlight 4 is selected as the
target version of Silverlight 4.

Right-click on SilverlightEmployeeBrowser.Web and select Add | New Item…. Add a Silverlight-enabled WCF Service by
selecting the Silverlight node under Visual C# and name it as EmployeeService. Click the Add button. Two files are
added, namely, EmployeeService.svc and EmployeeService.svc.cs.(Getting or Declaring Ours data manipulation here),

For ex in EmployeeService.svc.cs,
using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace SilverlightEmployeeBrowser.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeeService
{
[OperationContract]
public List<Employee> GetEmployees()
{
return new List<Employee>
{
new Employee
{
Name = "Peter",
Salary = 100,
Location = "Dallas",
Country = "USA"
},
new Employee
{
Name = "Rouville Fisher",
Salary = 200,
Location = "London",
Country = "United Kingdom"
},
new Employee
{
Name = "James",
Salary = 15000,
Location = "NY",
Country = "USA"
},
new Employee
{
Name = "Marciya",
Salary = 230,
Location = "NPD",
Country = "USA"
}
};
}
[DataContract]
public class Employee
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Location { get; set; }
[DataMember]
public string Country { get; set; }
[DataMember]
public double Salary { get; set; }
}
}
}
using System; using System.IO; using System.Data; using System.Linq; using System.Collections.Generic; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; namespace SilverlightEmployeeBrowser.Web { [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class EmployeeService { [OperationContract] public List<Employee> GetEmployees() { return new List<Employee> { new Employee { Name = "Peter", Salary = 100, Location = "Dallas", Country = "USA" }, new Employee { Name = "Rouville Fisher", Salary = 200, Location = "London", Country = "United Kingdom" }, new Employee { Name = "James", Salary = 15000, Location = "NY", Country = "USA" }, new Employee { Name = "Marciya", Salary = 230, Location = "NPD", Country = "USA" } }; } [DataContract] public class Employee { [DataMember] public string Name { get; set; } [DataMember] public string Location { get; set; } [DataMember] public string Country { get; set; } [DataMember] public double Salary { get; set; } } } } |
Next Right Click SilverlightEmployeeBrowser and select Add service reference and add Employeeservice we build,

Open MainPage.xaml and design it necessary,
for ex,
<UserControl x:Class="SilverlightEmployeeBrowser.MainPage"
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"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Width="400" Height="300"
Background="SkyBlue">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ComboBox x:Name="EmployeeComboBox" Width="250"
SelectionChanged="EmployeeComboBox_SelectionChanged"
DisplayMemberPath="Name"
VerticalAlignment="Center">
</ComboBox>
<Grid x:Name="EmployeeDetailGrid" Grid.Row="1"
VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="NameTextBlock"
Grid.Row="0"
Grid.Column="0"
FontWeight="Bold"
Text="Name: "
HorizontalAlignment="Right">
</TextBlock>
<TextBlock x:Name="NameValueTextBlock"
Grid.Row="0"
Grid.Column="1"
Text="{Binding Name}">
</TextBlock>
<TextBlock x:Name="LocationTextBlock"
Grid.Row="1"
Grid.Column="0"
FontWeight="Bold"
Text="Location: "
HorizontalAlignment="Right">
</TextBlock>
<TextBlock x:Name="LocationValueTextBlock"
Grid.Row="1"
Grid.Column="1"
Text="{Binding Location}">
</TextBlock>
<TextBlock x:Name="CountryTextBlock"
Grid.Row="2"
Grid.Column="0"
FontWeight="Bold"
Text="Country: "
HorizontalAlignment="Right">
</TextBlock>
<TextBlock x:Name="CountryValueTextBlock"
Grid.Row="2"
Grid.Column="1"
Text="{Binding Country}">
</TextBlock>
<TextBlock x:Name="PriceTextBlock"
Grid.Row="3"
Grid.Column="0"
FontWeight="Bold"
Text="Salary: "
HorizontalAlignment="Right">
</TextBlock>
<TextBlock x:Name="SalaryValueTextBlock"
Grid.Row="3"
Grid.Column="1"
Text="{Binding Salary}">
</TextBlock>
</Grid>
</Grid>
</UserControl>
<UserControl x:Class="SilverlightEmployeeBrowser.MainPage" 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" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Width="400" Height="300" Background="SkyBlue"> <Grid.RowDefinitions> <RowDefinition Height="50"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <ComboBox x:Name="EmployeeComboBox" Width="250" SelectionChanged="EmployeeComboBox_SelectionChanged" DisplayMemberPath="Name" VerticalAlignment="Center"> </ComboBox> <Grid x:Name="EmployeeDetailGrid" Grid.Row="1" VerticalAlignment="Top"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock x:Name="NameTextBlock" Grid.Row="0" Grid.Column="0" FontWeight="Bold" Text="Name: " HorizontalAlignment="Right"> </TextBlock> <TextBlock x:Name="NameValueTextBlock" Grid.Row="0" Grid.Column="1" Text="{Binding Name}"> </TextBlock> <TextBlock x:Name="LocationTextBlock" Grid.Row="1" Grid.Column="0" FontWeight="Bold" Text="Location: " HorizontalAlignment="Right"> </TextBlock> <TextBlock x:Name="LocationValueTextBlock" Grid.Row="1" Grid.Column="1" Text="{Binding Location}"> </TextBlock> <TextBlock x:Name="CountryTextBlock" Grid.Row="2" Grid.Column="0" FontWeight="Bold" Text="Country: " HorizontalAlignment="Right"> </TextBlock> <TextBlock x:Name="CountryValueTextBlock" Grid.Row="2" Grid.Column="1" Text="{Binding Country}"> </TextBlock> <TextBlock x:Name="PriceTextBlock" Grid.Row="3" Grid.Column="0" FontWeight="Bold" Text="Salary: " HorizontalAlignment="Right"> </TextBlock> <TextBlock x:Name="SalaryValueTextBlock" Grid.Row="3" Grid.Column="1" Text="{Binding Salary}"> </TextBlock> </Grid> </Grid> </UserControl> |
In MainPage.xaml.cs,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightEmployeeBrowser
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
EmployeeService.EmployeeServiceClient EmpClient = new EmployeeService.EmployeeServiceClient();
EmpClient.GetEmployeesCompleted +=new EventHandler<EmployeeService.GetEmployeesCompletedEventArgs>(EmpClient_GetEmployeesCompleted);
EmpClient.GetEmployeesAsync();
}
void EmpClient_GetEmployeesCompleted(object sender,EmployeeService.GetEmployeesCompletedEventArgs e)
{
EmployeeComboBox.ItemsSource = e.Result;
}
private void EmployeeComboBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
EmployeeDetailGrid.DataContext = (sender as ComboBox)
.SelectedItem as EmployeeService.EmployeeServiceEmployee;
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightEmployeeBrowser { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); EmployeeService.EmployeeServiceClient EmpClient = new EmployeeService.EmployeeServiceClient(); EmpClient.GetEmployeesCompleted +=new EventHandler<EmployeeService.GetEmployeesCompletedEventArgs>(EmpClient_GetEmployeesCompleted); EmpClient.GetEmployeesAsync(); } void EmpClient_GetEmployeesCompleted(object sender,EmployeeService.GetEmployeesCompletedEventArgs e) { EmployeeComboBox.ItemsSource = e.Result; } private void EmployeeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { EmployeeDetailGrid.DataContext = (sender as ComboBox) .SelectedItem as EmployeeService.EmployeeServiceEmployee; } } } |
Conclusion:
Please use forum for further more doubts/discussion/queries for this article
Happy coding.
Join the forum and make money by posting answers or discussing various topics here !

Thankful i recently found this amazing site, will make sure to save it so i can look at often.
When I try to add the service it gives me an error
for me the above code is working fine,can you please post here what error you got?
Wow this is a great resource.. I’m enjoying it.. good article
Very useful information. I appreciate your effort, very well written article.