ADO.Net DataServices and Silverlight

June 1, 2009 at 6:54 pm 1 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.

  1. Open VS2008 and select a ASP.Net Web Site, name it “PubsADOService”.
  2. Remove the Default.Aspx that is already added in the project.
  3. Right click on the web site in the “Solution Explorer” and select “Add New Item”.
  4. Select “ADO.Net Entity Data Model”. Name it, Pubs.edmx
  5. Click “Yes” to place the code in “ASP_Code” folder.
  6. Select “Generate from DataBase”, click “Next”.
  7. Select/create the Connection.
  8. Clik “Next”.
  9. 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.

  1. Create a new Silverlight project in VS2008. Name it, “Client_PubsDataService”.
  2. Click “Yes” to add an asp.net project to host the Silverlight application.
  3. Right click on the Client_PubsDataService in the solution explorer and select “Add Service Reference….”.
  4. Switch to PubsDataService created in above steps and run it.
  5. 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/
  6. Keep this application running and switch to “Client_PubsDataService” application.
  7. Pate the copied URL in the “Address” field of the “Add Service Reference….” Dialog box and click “Go”.
  8. The “PubsDataService” will be identified and on expanding it, you will see all the entities that are exposed by the service.
  9. 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)

  1. 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:

 ADO_Output

Advertisements

Entry filed under: ADO.Net DataServices, Silverlight. Tags: , , , .

Accessing ScriptableType objects of Silverlight from JavaScript Multi Paged Silverlight application consuming ADO.Net Data services

1 Comment Add your own

  • 1. best pest management  |  July 24, 2013 at 9:56 pm

    I am regular reader, how are you everybody?
    This piece of writing posted at this web site is in fact good.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts


%d bloggers like this: