Introduction:
Data Access has always been the heart and sole of any application. We saw ADO in classic Asp and ADO .NET in Asp .net 1.1. We also saw the data access using the Microsoft .NET application blocks, which I personally love :). In this article we will see how we can access data using classes and objects provided in Asp .net 2.0 (Whidbey).
Types of Data Sources control in Asp .net 2.0:
Types of Data Sources
ASP. NET 2.0 provides several distinct data source objects that are used to construct a common interface framework for data-bound controls. The data sources’ objects are used to manipulate different underlying structures, from databases and in-memory objects to XML files, and provide abstract data manipulation functionality for controls.
AccessDataSource
If you use a Microsoft Access database in your application, you can use the System.Web.UI.WebControls.AccessDataSource to perform insert, update, and delete operations on your data. The Access database is a minimal database that provides basic relational storage. Because it is simple and easy to use, however, Access forms the data storage layer for many small Websites. Although Access doesn’t offer the full features of a relational database such as SQL Server, its simplicity and ease of use make it very valuable in prototype or rapid application development (RAD) scenarios.
SqlDataSource
For a more robust database implementation that leverages the extensive feature provided by Microsoft SQL Server, the SQLDataSource is provided with ASP .NET 2.0. The configuration of a SQLDataSource is more complex then that of the AccessDataSource, and is intended for enterprise applications that require the features provided by a true database management system (DBMS).
ObjectDataSource
The System.Web.UI.WebControls.ObjectDataSource is used in scenarios where you need to implement a data access layer to provide better encapsulation and abstraction. Instead of binding to a database, the ObjectDataSource control lets you bind to a specific data layer in the same manner by which you bind to the database using other controls. The ObjectDataSource control can bind to any method that returns a DataSet or an IEnumerable object (for example, a DataReader or a collection of classes).
DataSource Control:
The heart and sole of the data access in Asp .net 2.0 is the DataSource Control. Let’s see a simple example in which you will find out what I am saying. Say, we are using asp .net 1.1 and we need to populate a dropdownlist with the data from the database. We can achieve this using the following code:
string connectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"]; SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter ad = new SqlDataAdapter("SELECT CategoryID,CategoryName FROM Categories", myConnection); DataSet ds = new DataSet(); ad.Fill(ds, "Categories"); DropDownList1.DataSource = ds; DropDownList1.DataBind();
Now, this is a lot of code to just fill a dropdownlist right? Let’s see how we fill it the way using Asp .net 2.0.
First just drag and drop the SqlDataSource control anywhere on the form. The wizard will guide you to several forms. The form is quite simple so I won’t go in details of the wizard. Just one thing to note is that the wizard will also ask you that if you like the connection string to be written in the web.config file. If you select yes than an entry in the web.config file will automatically be placed which I find very cool :).
Now that you have set up your SqlDataSource control you are ready to bind controls to the datasource. Just drag a DropDownList control on the form. As soon as you drop the control it will appear with smart tags. The image below shows the dropdownlist with smart tags.
You can choose “Choose Data Source option” where you can select your SqlDataSource control. After you choose your data source you can choose what text and value to display in the DropDownList.
Now run your application and you will be amazed to see that the dropdownlist is now filled with the contents that you have specified in the SqlDataSource object. You did not even have to write a single line of code and you achieved what took 5-6 lines in Asp .net 1.1 using Visual Studio.NET 2003.
SqlDataSource object does not only limit to a Sql Server database but you can use Access database, DataSet and Xml files and many more.
Let’s now see some other data sources:
Using Xml Data Source object with a Tree Control:
For this we will use Xml file which will contain the following lines:
<?xml version="1.0" encoding="utf-8" ?>
<Parent>
<child>
</child>
</Parent>
As you can see this is a very simple xml file. Now to display the contents of the xml file we will use the tree control. One of my favorite controls. Now see how easy it is to display the contents of the xml file to the tree control.
Just drag and Drop the tree control on the form and as soon as you drop it will will show the smart menu asking for the datasource. Simply assign it the datasource, you can also make your tree control look pretty by selecting one of the auto format available.
And that’s it now run the application and you will see something like this depending upon the data which you put in the Xml file.
See how cool it is. You can use XmlDataSource object with other controls too. You can also use the XmlDataSource object to bind the data to the dropdownlist.
Lets use another type of control now its called FormViewControl
FormViewControl:
As the name says its basically a control which you can use to display information in the form of forms. Just drag and drop the control on the web form. The control can be found under the data controls section. As soon as you drop it will ask for the SqlDataSource. Gently, assign it the datasource and enable paging if you wish.
Here is a small screen shot of the FormView control when you bind it to the datasource.
Isn’t this just too cool :). When using DataList or DataGrid in Asp .net 1.1 we had to write a lot of code to make it work. But now its too simple.
Caching in Asp.net 2.0:
We all remember our old friend caching don’t we? In Asp .net 1.1 when we applied caching for the datagrid control or any databound control we had to go back and check that if the database contents have been changed. If they were changed than we will apply the changes in our databound control. But Asp .net uses callbacks in caching. Which means that whenever the data changes it will sent a notification to the user saying that hey the data has been changed and it will automatically bind the new contents to the dropdownlist or any other databound controls.
I hope you liked this article.
Attachments:
Project Files: DataAccess_sample.zip