databasedev.co.uk - database solutions and downloads for microsoft access

ASP.NET

Working with the New Data Source Controls in ASP.NET (Part II)

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

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>

The LinqDataSource Control

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 in ASP.NET 3.5

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).

Conclusion

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)

The Author

Joydip Kanjilal is a Microsoft MVP in ASP.NET.

He has more than 12 years of industry experience in IT with more than six years in Microsoft .NET and its related technologies.

He has authored articles for some of the most reputable sites, including http://www.asptoday.com, http://www.devx.com, http://www.aspalliance.com, http://www.aspnetpro.com, http://www.sql-server-performance.com, and http://www.sswug.com.

Many of these articles have been selected at http://www.asp.net, Microsoft’s official site for ASP.NET. Joydip was also a community credit winner at http://www.community-credit.com a number of times.

He is currently working as a Lead Architect in a reputable company in Hyderabad, India. He has years of experience in designing and architecting solutions for various domains. His technical strengths include, C, C++, VC++, Java, C#, Microsoft .NET, AJAX, Design Patterns, SQL Server, Operating Systems, and Computer Architecture.

Joydip blogs at http://aspadvice.com/blogs/joydip and spends most of his time reading books and blogs, and writing books and articles. His hobbies include watching cricket and soccer and playing chess