New Data Source Controls in ASP.NET
ASP.NET 2.0 and beyond comes in with a bundle of new data source controls which you can use to implement CRUD (Create, Read, Update, and Delete) operations in your applications with the least amount of code. You can use these controls to declaratively bind data in your applications. You use the ObjectDataSource control to bind data to the data controls from generic business objects. The AccessDataSource, SQLDataSource, and XMLDataSource controls are used to bind data to the ASP.NET data controls from Access, SQL Server Database, or XML data sources, respectively. This article takes a quick look at the new data source controls introduced as part of ASP.NET. It also discusses how you can work with the controls to leverage their awesome power in your applications.
ASP.NET 2.0 ships with a lot of data source controls, namely:
XMLDataSource Control: This is used for binding data to XML files.
ObjectDataSource Control: This is used for binding data to custom business objects.
SqlDataSource Control: This is used for binding data to a Microsoft SQL Server database.
AccessDataSource: This is used for binding data to a Microsoft Access database.
LinqDataSource Control: This is used for binding data to Linq data sources.
SiteMapDataSource Control: This control loads a site map and then it exposes it to other controls like the TreeView and SiteMapPath controls
DataSetDataSource Control: The DataSetDataSource control allows you to manipulate an XML document same as a dataset.
Working with the Data Source Controls
The sequence of steps for working with these controls is:
1. Create a new web site
2. Drag and Drop one of the data source controls from the toolbox onto your web form
3. Create a new data control (GridView, Repeater, etc)
3. Configure the data source control
4. Bind data from the control to another data control (GridView, Repeater, etc)
5. Execute the application
To use the data source controls, you first need to have a data control placed in your page so that you can use it to bind data. As an example, take a GridView control and use it to bind data to these data source controls. Here is the mark-up code for the simplest form of a GridView control.
<asp:GridView runat="server"
DataSourceID="ObjectDataSource1"
ID="GridView1" BackColor="LightGray"
AllowPaging="true"/>
Note that the DataSourceID property of the GridView control should refer to the data source control that you want it to be related to. In this example we have taken an ObjectDataSource control. The MSDN states, "The ASP.NET ObjectDataSource control represents a middle-tier object with data retrieval and update capabilities. The ObjectDataSource control acts as a data interface for data-bound controls such as the GridView, FormView, or DetailsView controls. You can use these controls to display and edit data from a middle-tier business object on an ASP.NET Web page." Unlike the SqlDataSource control that supports two-tier architecture, the ObjectDataSource supports three tier architecture as you can bind data to your business objects and perform CRUD operations seamlessly without having to write much code at all.
Here is the mark-up code of our ObjectDataSource control.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetEmployee" TypeName="Employee" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="DeptID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Once your ObjectDataSource and the GridView controls are properly configured, you are done! The GridView control displays the data from the employee table. You needn’t write any code at all. Awesome! Isn’t it?
Among the other data source controls, the LinqDataSource control is special. It allows you to bind data to Linq data sources seamlessly. 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.”
Here is how the mark-up code of the LinqDataSource control looks like once you place it in your web form and configure it.
<asp:LinqDataSource
ContextTypeName="EmployeeDataContext"
TableName="Employee"
ID="myLinqDataSource"
runat="server">
</asp:LinqDataSource>
And, here is the GridView control that will display the data from the employee table in a tabular format.
<asp:GridView
DataSourceID="myLinqDataSource"
AutoGenerateColumns="false"
ID="GridView1"
runat="server">
<Columns>
<asp:BoundField DataField="EmployeeID" />
<asp:BoundField DataField="EmployeeName" />
<asp:BoundField DataField="EmployeeAddress" />
</Columns>
</asp:GridView>
Note that the ContextTypeName property of the LinqDataSource control is used to map it to the entity class that represents the database. The TableName property is used to map the control to the class that represents the database table in use.
Conclusion
In this article we discussed the new data source controls introduced in ASP.NET. You can learn more on these data source controls in my book titled, ASP.NET Data Presentation Controls Essentials (Packt Publishing). Here is the link to the book: http://www.packtpub.com/asp-net-data-presentation-controls/book
Happy reading!