Introduction to ADO.NET 2.0 Data Source Controls

Introduction

ASP.NET 2.0 provides new data source and data-bound server controls, which allow developers to write full-fledged data-driven applications without writing even a single line of code. This article is an introduction to these new data source controls. In my forthcoming articles, I will cover various data bound controls such as GridView, DetailsView, and FormView and how these controls use data-source controls to display data.

Microsoft Architects have proved in previous versions of ASP.NET that they are working hard to help programmers write less and less code, which is reused every where. For example, writing code for a database connection and setting up data access functionality (select, update, and delete) is very common functionality every programmer has to go through. Now ASP.NET 2.0 extends this functionality by providing built-in controls. These controls hardly require any code for common functionality.

Data source controls provides data access functionality including selecting, updating, deleting, and inserting data without writing any lines of code. As a developer, you can always access these controls' functionality programmatically. ASP.NET 2.0 has six data source controls and each of these data source controls is designed to work for a specific data source to provide better performance and flexibility. However there are more data source controls planned to release in next versions of ASP.NET. ASP.NET 2.0 has following data source controls

  • SqlDataSource
  • AccessDataSource
  • ObjectDataSource
  • DataSetDataSource
  • XmlDataSource
  • SiteMapDataSource

All of these data source controls are inherited from DataSourceControl class, which provides common functionality for all of these data source controls.

Data Source Controls

There are six data source controls and each of them is designed to work with a specific data source. The following table defined data source controls -

Data Source Control Description Data Source
SqlDataSource Designed to work with SQL Server databases. It uses Sql Server .NET data provider internally. Sql Server .NET data provider classes are defined in the System.Data.SqlClient namespace. SQL Server 2000 or later.
AccessDataSource Designed to work with Microsoft Access. It uses OleDb data provider internally. OleDb data provider classes are defined in the System.Data.OleDb namespace. Microsoft Access
ObjectDataSource Designed to work with objects. Object (usually classes provide a well-defined structure)
XmlDataSource Designed to work with XML documents. XML documents
SiteMapDataSource Designed to work with SiteMap objects, which is a new concept in ASP.NET 2.0. SiteMap
DataSetDataSource Designed to work with DataSet objects. DataSet

I am surprised not to see Oracle and ODBC data source controls, which are major requirements for non-Microsoft databases. I guess we can use these databases using traditional approach using Oracle and ODBC data providers.

Adding Data Source Controls using Visual Studio 2005

You can add SqlDataSource control by simply dragging control from Toolbox to a page. See Figure 1.

DataSourceControls1.jpg

Figure 1. SqlDataSource in Toolbox

When you drop SqlDataSource to a page, you will see Configure Data Source link. See Figure 2.

DataSourceControls2.jpg

Figure 2. Configure Data Source

Clicking this link launches Configure Data Source wizard, which allows you to configure a data source connection. See Figure 3. This wizard allows you to create a new connection or use existing connections by selecting from data connection drop down list.

Datasourcecontrols.jpg

Figure 3. Configure Data Source wizard

Clicking New button of Configure Data Source wizard launches Connection Properties wizard, which allows you to create a new connection. See Figure 4.

DataSourceControls3.jpg

Figure 4. Connection Properties wizard.

The next dialog of Configure Data Source wizard allows you to specify the variable name of the connection string. See Figure 5.

DataSourceControls5.jpg

Figure 5. Connection string page

The following dialog allows you to specify the SELECT statement. Here you can select a database table and its columns and add WHERE and ORDER BY clauses. See Figure 6.

DataSourceControls6.jpg

Figure 6. Configure Select Statement dialog.

The next dialog tests the data. See Figure 7.

DataSourceControls7.jpg

Figure 7. Test Query dialog.

Now this entire action adds the following code to the HTML file:

If you do not have Visual Studio, you can create SqlDataSource control in HTML by editing the HTML code. As you can see from this code, tag <asp:SqlDataSource> represents the SqlDataSource control and like other controls, you can set its parameters in HTML file. The above code sets SelectCommand and ConnectionString parameters of SqlDataSource control.

You can also set the controls parameters from Properties window. As you can see from Figure 8, you can set various properties of SqlDataSource including ConnectionString and SelectQuery.

DataSourceControls11.jpg

Figure 8. SqlDataSource properties.

If you InsertQuery property, it launches Command and Parameter Editor, where you can add and remove parameters. See Figure 9. From this dialog, you can also launch Query Builder, which allows you to create a SQL query.

DataSourceControls12.jpg

Figure 9. Command and Parameter Editor

Summary

ASP.NET version 2.0 provides new data source and data bound controls. In this article, I discussed data source controls and how to use them in Visual Studio 2005. In my forthcoming articles, I will be discussing rest of data source controls and how they can be used with data bound controls.

Up Next
    Ebook Download
    View all
    Learn
    View all