Skip Navigation LinksHome > Categories > Code from a Category

Building DAC with Execution Time in ASP.NET 3.5 and C#

User Name: codelecturer
Name: Mike Chauhan
Contact Me:
Home Page:
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 1315
Add Date: 12/04/2012
This article will show you how to build your own Data Access Component and how to retrieve the time taken to execute.
Instead of using ASP.NET's built-in controls to retrieve our data, we can build our own Data Access Components. In this tutorial, we will look at how we can do this, and how we can also retrieve the duration of data retrieval.

This can be useful when dealing with large amounts of data. For this example, we will be working with a SQL database, and create our own class to retrieve the data. We will be using the Object Data Source to interact with our class, and a GridView to display the data.

The database we will be working with will have just one table, and three columns - id, name and age. Once created, we will add some sample data to use.

When our database is ready to go, we can start building our class that will retrieve data from our database. The class will have a method to retrieve all records from the database and input into a List. Our class will look something like this:

The GetAll method uses a List to collect all data from the database and then using a loop, we add each record from the database into the results List we created. This is the List that is then returned. We will use this method to select data, using the ObjectDataSource. We can now build our ASPX page like so:

Notice that we have assigned the GetAll method to our SelectMethod attribute of our ObjectDataSource, and the TypeName is the name of the Class. We also have a method that fires on the OnSelected event. This means that when the ObjectDataSource selects data (through the class), the following code is processed:

This is where we output the time it took to retrieve the data.
The entire code-behind will look something like this:

The entire code of the class is as follows:

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.