In the earlier part in this series, Working with the New Data Source Controls in ASP.NET (Part I), we discussed the ObjectDataSource, AccessDataSource and SqlDataSource controls. In this concluding part in this series of articles on data source controls, we will discuss the XmlDataSource, LinqDataSource and the newly added EntityDataSource controls.
The XmlDataSource control is used to declaratively connect to an Xml data source and perform CRUD operations against it. You can use the XmlDataSource control to support declarative data binding to XML data sources. The XmlDataSource control also supports hierarchical data bound controls like Tree view and Menu controls and binding data to the ASP.NET tabular data bound controls like Repeater, GridView and Data List. Here is how you can use the XmlDataSource control with a Repeater control:
<asp:Repeater ID="Repeater" runat="server" DataSourceID="XmlDataSource1"> <ItemTemplate> <%# XPath("@fname") %><br /> <%#XPath("@lname")%><br /> <%#XPath("@basic")%><br /> </ItemTemplate> </asp:Repeater> <asp:XmlDataSource ID="XmlDataSource" runat="server" DataFile="~/Employee.xml" XPath="EmployeeRecords/Employee"> </asp:XmlDataSource>
And, here is the content of the Employee.xml file:
<?xml version="1.0" encoding="utf-8" ?> <EmployeeRecord> <Employee FName="Joydip"> <LName>Kanjilal</LName> <Basic>2000</Basic> </Employee> <Employee FName="Manashi"> <LName>Kanjilal</LName> <Basic>1000</Basic> </Employee> <Employee FName="Jini"> <LName>Oindrilla</LName> <Basic>3000</Basic> </Employee> </EmployeeRecord>
You can use the LinqDataSource control both programmatically as well as declaratively in your code.
What is LINQ anyway?
Language Integrated Query, or LINQ as it is called, ships with the Microsoft .NET Framework and provides an easy way to access data, execute queries, and model your databases using .NET objects. According to MSDN, "Language-Integrated Query (LINQ) is a set of features in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store."
In using this control, you can eliminate the data-store-specific coding in your applications. You can simply use this control to retrieve and bind data to your ASP.NET data controls from any LINQ-enabled data source. In essence, you can use the LinqDataSource control to perform CRUD operations by leveraging the benefits of the unified programming model that LINQ provides.
Here’s how the markup code of a typical LinqDataSource control looks like:
<asp:LinqDataSource runat="server" ContextTypeName="EmployeeDataContext" TableName="Employee" ID="LinqDataSourceControl1"> </asp:LinqDataSource>
Once done, you can now create an ASP.NET data control and then associate the LinqDataSource control to bind data. Here is an example:
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSourceControl1" > </asp:GridView>
The EntityDataSource control is a newly added data source control that can connect to an ADO.NET Entity Data Model (EDM) to perform CRUD operations against the EDM. EntityDataSource control is a newly added data source control in ASP.NET 3.5 and supports data binding in web applications using an Entity Data model (EDM) as the source of the data.
The MSDN states, "The ADO.NET EntityDataSource control supports data binding scenarios in Web applications that use the ADO.NET Entity Framework. Like the Entity Framework, the control is available as part of the .NET Framework 3.5, beginning with SP1. Users accustomed to the design-time model of ASP.NET data binding controls will find the programming surface of the EntityDataSource control similar to the SqlDataSource, LinqDataSource, XmlDataSource, and ObjectDataSource controls."
You can know more on the ADO.NET Entity Framework, Entity Data Model and EntityDataSource control from my book, titled, "Entity Framework Tutorial" (Packt Publishing).
In this series of articles we have had a look at the new data source controls in ASP.NET and how they can be used in our applications. We also have had a look at the newly introduced EntityDataSource control in ASP.NET 3.5 and how it can be used to connect to the ADO.NET Entity Data Model.
You can find more information on these controls in my book titled, "ASP.NET Data Presentation Controls Essentials" (Packt Publishing)