ADO.Net DataServices and Silverlight
June 1, 2009 at 6:54 pm Leave a comment
This post describes the complete steps to create an ADO.Net DataServices exposing the SQL Data, using Visual Studio 2008 Professional version and also describes the steps to be taken to create a Silverlight client application to access the data exposed by ADO.Net Data Services. In case, if you’ve got any specific requirement, please feel free to write to me at HarishSuhanda@Gmail.com
Creating the ADO.Net Data Services to expose the PUBS database.
- Open VS2008 and select a ASP.Net Web Site, name it “PubsADOService”.
- Remove the Default.Aspx that is already added in the project.
- Right click on the web site in the “Solution Explorer” and select “Add New Item”.
- Select “ADO.Net Entity Data Model”. Name it, Pubs.edmx
- Click “Yes” to place the code in “ASP_Code” folder.
- Select “Generate from DataBase”, click “Next”.
- Select/create the Connection.
- Clik “Next”.
- Select the Name of the Tables that you want to expose via ADO.Net Data Service. For this demo select all the tables.
10. Rename the Entity Set Names of all the tables by adding “Set”. Thus, the entitis name and Entity Set Name should be, Titles and TitlesSet, author and authorSet like wise…
11. Click “Save All”.
12. Open the Solution Explorer and repeat step 3 to add “New Item”.
13. Select “Ado.Net Data Service”. Name it “PubsDataService.svc”. Click “Add” button.
14. In the “PubsDataService.svc.vb” or “PubsDataService.svc.cs”, whichever application (.vb is for Visual Basic and .cs is for C#). Replace the code,
Inherits DataService(Of [[class name]])
With
Inherits DataService(Of pubsModel.pubsEntities)
15. In the InitializeService method, un comment the code to SetEntityAccessRule. (By default all the entities defined in the EntitiyModel has restrictsions. The will be blocked by default unless the EntitiyAccessRule is not defined)
16. Replace “MyEntitySet” with the name of the entityset that you want to expose. To expose all the entities, set
config.SetEntitySetAccessRule(“*”, EntitySetRights.All)
17. Click “Save All”. Now the PubsDataService is avalable to be hosted.
18. Run the PubsDataService web site.
Create the Silverlight client to access the PubsDataService created above.
- Create a new Silverlight project in VS2008. Name it, “Client_PubsDataService”.
- Click “Yes” to add an asp.net project to host the Silverlight application.
- Right click on the Client_PubsDataService in the solution explorer and select “Add Service Reference….”.
- Switch to PubsDataService created in above steps and run it.
- Copy the URL of the PubsDataService.svc file, which is something like (except the port number will be different) http://localhost:49165/PubsADOService/PubsDataService.svc/
- Keep this application running and switch to “Client_PubsDataService” application.
- Pate the copied URL in the “Address” field of the “Add Service Reference….” Dialog box and click “Go”.
- The “PubsDataService” will be identified and on expanding it, you will see all the entities that are exposed by the service.
- Change the name of the service to “PubsServiceReference” and click “OK”
10. Open Page.xaml and add a ComboBox to the “LayoutGrid”.
11. Add 2 Rows to grid, using
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
12. Place a “DataGrid” from toolbox below the ComboBox.
13. Assign a name to the ComboBox, bind the itemssource and attach the SelectionChanged event. <ComboBox x:Name=”lstEntities” ItemsSource=”{Binding}” SelectionChanged=”lstEntities_SelectionChanged”></ComboBox>
14. Assign a name to the DataGrid and bind the itemssource property. <data:DataGrid x:Name=”GridDisplayData” Grid.Row=”1″ ItemsSource=”{Binding}”></data:DataGrid>
15. Switch to code view of the Page.Xaml. Define a global variable named “PubsService” of type PubsServiceReference.pubsEntities
16. In the constructor, after the InitializeComponents method, initialize the PubsService by passing the Uri object. The Uri object should be created using two parameters, the url of the PubsDataService ie, http://localhost:49165/PubsADOService/PubsDataService.svc/ and Uri type which is Absolute.
17. Add the name of the entities to the lstEntities that you disposed via the Service. Using lstEntities.Items.Add(“SalesSet”), lstEntities.Items.Add(“StoreSet”) like wise all the entities.
18. In the lstEntities_SelectionChanged event, put a Select Case statement, Select Case lstEntities.SelectedIndex. For each selectedIndes write the code to fetch the data from the EntitySet using the code below:
Like for Authors:
Case 1
Dim dsq As System.Data.Services.Client.DataServiceQuery(Of PubsServiceReference.author)
dsq = CType(PubsService.authorSet, System.Data.Services.Client.DataServiceQuery(Of PubsServiceReference.author))
dsq.BeginExecute(New AsyncCallback(AddressOf Authors_LoadComplete), dsq)
- Define the Authors_LoadComplete event using following code and bind the result to the Grid.
Private Sub Authors_LoadComplete(ByVal ar As IAsyncResult)
Dim qry As System.Data.Services.Client.DataServiceQuery(Of PubsServiceReference.author) = _
CType(ar.AsyncState, System.Data.Services.Client.DataServiceQuery(Of PubsServiceReference.author))
Dim Authors = qry.EndExecute(ar).ToList
GridDisplayData.DataContext = Authors
GridDisplayData.AutoGenerateColumns = True
End Sub
OutPut:

Entry filed under: ADO.Net DataServices, Silverlight. Tags: ADO.NET DataServices and Silverlight, Binding Silverlight DataGrid to ADO.Net DataServices, Silverlight2.0, Working with ADO.Net DataServices and Silverlight.
Trackback this post | Subscribe to the comments via RSS Feed