ADO.NET Data Services With ASP.NET
Introduction
In this tutorial we will look at how ADO.NET Data Services can be used to create services which are consumed by ASP.NET client. We will use Adventure works Lite database as an example to demonstrate the concepts. Code for this article can be downloaded at the bottom of the article.
ADO.NET Data Services as the name says is a services layer between your application and an underlying data source. It uses REST principals to access and persist information to and from the data source. Services layer itself can be created very easily. Most of the work is done by Visual Studio for you.
In this tutorial we will build an application for AdventureWorks Lite database. We will build a page which let’s a user browse through products. The reason for picking up AdventureWorks Lite as an example is so that we can work with a variety of data including images. If you do not have AdventureWorks Lite then you can download it here. For our application we will work with this data model

Creating Service
Creating a ADO.NET Data Service in Visual Studio is a no brainer. All we need to do is add a new item of type ADO.NET Data Service, give it an appropriate name and we are done.

Our ADO.NET Service will interact with some kind of data source and Entity Framework perfectly fits that purpose. We now need to create a EDM (Entity Data Model) which includes tables we want to interact with via our services. If you never worked with Entity Framework then this quickstart will get you going.
By creating a service and our Entity Data Model we have put together two major components of our solution. Before we can hit F5 to see our service running, there are few things we need to put in place. We must tell our service that there is a Data Layer that we want it to use. Now all ADO.NET Data Services inherit from DataService<T> which can be found in System.Data.Services namespace. The code produced by Visual Studio for our service leaves a placeholder for <T> with a comment. All we need to do is specify the correct type. Our class declaration for our service should look like this.
public class AdventureWorksService : DataService<AdventureWorksEntities>
Here we have established integration between our service and our data source. Next thing we need to do is configure appropriate access levels for our service and its operations. Code below when executed initializes our service to have all possible access over all entities defined in our EDM and all access over all operations. Note the asterix, basically we are saying here that include all Entity Sets and all operations.
public static void InitializeService(IDataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); }
We are now ready to go and hit F5. If we set AdventureWorksService.svc as a start page and run our project we can see our service running. Now that we have our service running, we will start building our ASP.NET pages which will query our service, receive and display data.

Creating Proxy Objects
Our ASP.NET application will need to access the service through a proxy. Creating a proxy is very easy. After making sure that our service is running, we need to add a service reference to our serivce. This can be done this way.

It is also a good idea to change the Namespace from ServiceReference1 to AdventureWorksService.
Creating Web Page To Display Data
We can now make calls to our service and retrieve data. First thing we need to do is load data from ProductCategory and bind it to a dropdown. But before that let’s have a look at what we will produce.

Getting back to binding our dropdown control. We should write a query which calls our service which in turns fetches data from database and delivers to us. One good thing about ADO.NET Data Services is that you can write LINQ queries with it. ADO.NET Data Services comes with a LINQ provider popularly known as LINQ To REST. Without LINQ support ADO.NET Data Services will be just too complicated and IMO useless. However LINQ support is limited. Here are things you cannot do:
- You cannot write queries which involve joins or sub queries.
- You cannot create anonymous types in your queries.
- Aggregates such as Count, Min, Max are not available.
- There is no GroupBy support available.
Besides the list of can’t do above, LINQ To REST still makes working with ADO.NET Data Services much easier than otherwise. Following code will make a service call and bind our drop down.
private void BindProductCategories() { DataServiceQuery<ProductCategory> productCategories = context.CreateQuery<ProductCategory>("/ProductCategory"); var query = from p in productCategories orderby p.Name select p; ddlProductCategory.DataSource = query; ddlProductCategory.DataBind(); }
We’d also like to refresh data in our grid when the user selects another category. This is done by BindProductsGrid method.
private void BindProductsGrid() { int productCategoryID = Convert.ToInt32(ddlProductCategory.SelectedValue); DataServiceQuery<Product> products = context.CreateQuery<Product>("/Product"); var query = from p in products where p.ProductCategory.ProductCategoryID == productCategoryID select p; gdvProducts.DataSource = query; gdvProducts.DataBind(); }
The way I bind the image is by using another page which just does Response.BinaryWrite. Here is the markup of default.aspx page which is the entire UI. You can play with it by downloading the entire solution. The markup is here just to make the post look good ;)
<form id="form1" runat="server"> <div> <div> Product Category <asp:DropDownList ID="ddlProductCategory" runat="server" DataValueField="ProductCategoryID" AutoPostBack="true" DataTextField="Name" OnSelectedIndexChanged="ddlProductCategory_SelectedIndexChanged"> </asp:DropDownList> </div> <br /> Products <br /> <div> <asp:GridView ID="gdvProducts" runat="server" CssClass="sample" Width="600px" AutoGenerateColumns="false" OnRowCreated="gdvProducts_RowCreated"> <Columns> <asp:BoundField DataField="ProductNumber" HeaderText="Number" /> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:TemplateField> <ItemTemplate> <asp:Image ID="Image1" runat="server" ImageUrl='ImageHelper.aspx' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </div> </form>
Conclusion
This was a brief introduction to ADO.NET Data Services. We looked at how to create a service and consume it in a ASP.NET website. We also looked at some limitations of LINQ provider used by ADO.NET Data Services. In future posts I will talk about other features of ADO.NET Data Services.
Download Code For This Article
18 Responses to ADO.NET Data Services With ASP.NET
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


[...] an earlier article I looked at how DO.NET Data Services can be used with ASP.NET. In this post I will talk about using [...]
[...] In this post we will look at Logical Operators in ADO.NET Data Services. These operators can be used as filter expressions in our requests and ADO.NET Data Services applies the filter before results are returned. We will use Data Services generated over Adventureworks Lite database. If you are new to ADO.NET Data Services and would like to know how to generate ADO.NET Data Service then please see this article. [...]
ADO.NET Data Services With ASP.NET | One .Net Way…
9efish.?????? – Trackback from 9eFish…
hello,sir
I want to know about Asp.net web service
Hello
sir,
Now I am using Asp.net,but I don’t know Ado.net data service clearly?
so,I want know .
Hi Hla,
Do you have any specific question in mind?
It’s a nice intro. But while linq is ok with me, not being able to create a join would seem to be very limiting. Is that a built in limitation of linq or is it a limitation of ado data services? Limitations like no joins and no aggregates would seem to be major showstoppers.
Spocko,
This is a limitation of ADO.NET Data Services. LINQ supports joins and aggregates very well. We should however see more additions to features of ADO.NET Data Services and I’m hoping that things missing in current release will be present in future releases, at least some of them.
Hi Deepak,
very nice intro….
I see that we are using hardcoded URL to access Data service in the “consumer” application. is there any way so that we dont have to hardcode the URL. AFAIK new Uri(“Products.svc”, UriKind.Relative) is a way but not working for me.
please let me know your thoughts on this.
Thanks
Akhilesh Bhale
Hi Akhilesh,
Services are available at addresses which are fixed. If you are concerned about using hardcoded URIs in code then putting them in config file can be another option.
Hi Deepak,
Thanks For the reply. what about those services which are in my solution. do i need to host them saperately and then use them or is it possible that i can reference them in my web application and use them??
Thanks,
Akhilesh Bhale
Hi Akhilesh,
I would recommend that you access services from their Uri in your project.
Dear Sir,
There is a question needs your help or discuss with you.
When I relese my website (the pubilsh url: http://localhost:1234/a.svc) , my ado.net data service can’t access the data as I remove the prot number.
Is there any method to let user can access url directly and access the DB successfully without specific port.
Thanks a lot.
Best regards,
Amanda Lee.
Hi Amanda,
It looks like you are running your service on the developer web server which is part of Visual Studio. That’s the reason you see that port number. You can easily change your service/project settings to use IIS which by default will use port 80.
Hope this helps.
Deepak
When i type DataServiceQuery productCategories =
context.CreateQuery(“/ProductCategory”);
the context.CreateQuery creates an instance of ObjectQuery . Why? Pls reply as early as possible.
Hi,
How to deploy the data service separately and access this deployed data service from the client application?
thanks,
suba
I cant find ADO.NET Data Service in the Add > New Items section. I am using Visual Studio 2010? Is is something you need install seperately or has it been replaced by WCF Data Service?
1 ice