TechBubbles Microsoft Technology BLOG

Data Binding for Silverlight Application

This post explains loading data into Silverlight form using ADO.NET Data Services service. We need to do the following tasks to achieve this

  • Creating an ADO.NET Data Services project
  • Creating a Silverlight application and referring the created service above

Creating the ADO.NET Data Services project 

 

1. Create an empty web application using Visual Studio 2010 as follows

image

2. Right click the project and say Add new item and select ADO.NET Entity Data Model project item

image

 

3. Choose Data Connection and give Model name AdventureWorksEntities

image

 

4. Here I am using AdventureWorks database to select the database object to bind to the controls in Silverlight form

image

 

5. Create the WCF Data Service to expose the data by right clicking on project item then select the WCF Data Service project item

image

 

6. Configure the DataService.svc by writing the following code in codebehind

 

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Data.Services;
   4:  using System.Data.Services.Common;
   5:  using System.Linq;
   6:  using System.ServiceModel.Web;
   7:  using System.Web;
 
   9:  namespace WebApp
  10:  {
  11:      public class DataService : DataService<AdventureWorksEntities>
  12:      {
  13:          // This method is called only once to initialize service-wide policies.
  14:          public static void InitializeService(DataServiceConfiguration config)
  15:          {
  18:       config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  21:          }
  22:      }
  23:  }

 

Save the details and build the project

 

7. Creating the Silverlight application by right clicking the solution explorer and give the name as SilverlightApp

image

8. The solution explorer might look like as follows

image

 

9.  Add service reference to Silverlight application as follows in the diagram

image

 

10. You can define the controls in Silverlight .xaml page by selecting the datasources from Data Menu in visual studio

image

 

11.  In the Data Sources window select the customers node from drop down and drag the grid to designer. It will look like something as follows

image

 

12. Add the following code in .xaml.cs file as follows

   1:          private AdventureWorksEntities Serviceref;
   2:          private System.Windows.Data.CollectionViewSource customersView;
   3:   
   4:          private void UserControl_Loaded(object sender, RoutedEventArgs e)
   5:          {
   6:   

7: Serviceref = new AdventureWorksEntities

(new Uri("http://localhost:5991/DataService.svc/"));

   8:   
   9:              customersView = this.Resources["customersViewSource"]
  10:              as System.Windows.Data.CollectionViewSource;
  11:              Serviceref.Customers.BeginExecute(result => customersView.Source 
  12:                  = Serviceref.Customers.EndExecute(result), null);
  13:   
  14:          }

13. You should be able to see the data binded to grid control

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud