Hello friends,
Nowadays I have just started blogging to share knowledge with others.
This is the post about loading data from Northwind database into SilverLight Datagrid
And I will use WCF and Entity Framework for that and you just have to following the below simple steps.
This is the very basic task and very basic application.

System Requirement:

IDE: Microsoft Visual Studio 2010
DATABASE: MSSQL 2005
You can find NorthWind Database from Below CodePlex Link
http://sqlserversamples.codeplex.com/#databases
http://archive.msdn.microsoft.com/northwind/Release/ProjectReleases.aspx?ReleaseId=1401

1. Create Silverlight Application

• FROM File Menu: File->New->Project->SilverLight Application


• Give Name to Application : SilverLight_LoadDataUsingWCF

• Now Press Ok

• It will Ask you for Host Your application in WebSite press Ok.

Now you can see two projects are created in SolutionExplorer with below name.

1. SilverLight_LoadDataUsingWCF
2. SilverLight_LoadDataUsingWCF.Web

2. Add ADO.NET entity Model(Model of M-V-VM Framwork)

• Right Click project SilverLight_LoadDataUsingWCF.Web from SolutionExplorer and Choose
• ADD-> New Item -> Ado.net Entity Data Model

• Give Name : ContactModel.edmx

• Press ADD button.
• Entity DataModel Wizard Dialogue box will open
• Select Generate From Database and Press Next
• Now It will asking you for Selecting Database. (I hope NorthWind Database is already hosted on your MSSQL Server if Not then download it and put it in your MSSQL Server)
• Selecting the NorthWind from ComboBox.(you can add new connection using New Connection… Button if it is not available in ComboBox)
• Now give Name to the Entity at very below in Dialogue box.
Name => ContactModelEntity and Press Next Button.
• It will Ask for Selecting Table (Which Database object do you want to include in your Model?)
• Expand the Tables Node and Select only Contacts Table and Press Finish Button.

That’s it you have Created Model and Entity which will be used in WCF Service.


3. Create WCF Services so we can consume it in our main project. (ContactMdoelEntity will consumed by this WCF service which we have created in Step2)



• Right Click project SilverLight_LoadDataUsingWCF.Web from SolutionExplorer and Choose
• ADD-> New Folder and Name It Service.
• Right Click Service Folder and Choose
• ADD-> New Item ->select Silverlight-enabled WCF Service.
• Name it with : ContactService.svc
• Now double click the ContactService.svc it will show you ContactService.svc.cs file.
• And Add below code inside ContactService class and remember don’t miss to add attribute OperationContract to GetContacts() Method.



OperationContract
public List<SubContact> GetContacts()
{

using(ContactModeEntity context = new ContactModeEntity())
{
return (from c in context.Contacts
select new SubContact()
{
ContactName = c.ContactName,
ContactTitle = c.ContactTitle,
ContactType = c.ContactType,
Country = c.Country,

}).Take<SubContact>(25).ToList<SubContact>();

}
}

public class SubContact
{
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string ContactType { get; set; }
public string Country { get; set; }
}


• Now we have created WCF services and it can be consume by any application by just adding service reference inside the project.

4. Now we will Add Service Reference in our main project(projectSilverLight_LoadDataUsingWCF) to consume WCF Service which we just have created in Step 3.

• Right Click project projectSilverLight_LoadDataUsingWCF from SolutionExplorer and Choose
• Add Service Reference…
• It will pop up Add Service Reference Dialogue Box.
• Click on Discover Button.
• It will show you available Services in Service Pan. It will also show the service which we have created in Step 3(ContactService).
• You can also see available operations in Operations Pan.
• Select ContactService.svc .
• Give the namespace name to Service in textbox in Add Service Reference Dialogue box.
• Have a look at below image



5. Write some code to use service so, add below code in MainPage.xaml.cs

public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Service.ContactServiceClient contactServiceClient = new Service.ContactServiceClient();

contactServiceClient.GetContactsCompleted += (s, ea) =>
{
dataGrid1.AutoGenerateColumns = true;
dataGrid1.ItemsSource = ea.Result;
};
contactServiceClient.GetContactsAsync();
}
}

6. Download Complete Demo Code

Last edited Oct 17, 2011 at 11:07 AM by PriteshAryan, version 3