When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, December 29, 2004

A Sneak Peak at Working with Data in ASP.NET 2.0

By Scott Mitchell

For More Information on Working with Data in ASP.NET 2.0...
For more up-to-date information on working with data in ASP.NET 2.0 check out the Accessing and Updating Data in ASP.NET 2.0 article series. This multi-part series covers the basics of the data source controls along with detailed instructions on using each of the five built-in data source controls.


The one constant in the field of computer science is change. Each month, it seems like, new technologies arise, old ones fall out of favor. New techniques for accomplishing the same old things arise. A new thing comes around, making the old thing seem less exciting. Let's face it - if you want a job that is static, one that you can completely master once and not need to continually learn new things, then anything even remotely technology involved is not a good career choice for you! Change in computer science, as with anything in life, is both good and bad. In the short run, change reduces your efficiency, as you have to relearn techniques and master new technologies. However, most change is beneficial in the long run, as the new thing, whatever it may be, is usually superior to the old thing in a myriad of ways.

That being said, for those of us who have worked with ASP.NET now for the past several years, another change is in the winds, moving from ASP.NET 1.x to ASP.NET 2.0. Fortunately, the downside of change is less than what it could be. Those who moved from classic ASP to ASP.NET no doubt would agree that such a transition involved a fairly steep learning curve. The move to ASP.NET 2.0 from ASP.NET 1.x is going to be easier because the underlying fundamentals between the two are the same. However, there are a slew of new things coming forward with ASP.NET 2.0. A whole lotta new things.

In this article I'll briefly discuss some of the bigger new things in ASP.NET 2.0, and focus in on one of the more relevant new things: the new and improved means for working with data in an ASP.NET 2.0 Web page. (This article was written during the Beta 1 time frame of ASP.NET 2.0; the technologies discussed may or may not make it to the final version of ASP.NET 2.0. You can download the latest 2.0 beta bits from http://labs.msdn.microsoft.com/.)

- continued -

A Taste of the New Things to Come

When creating ASP.NET 2.0 the ASP.NET Team focused on making the common tasks easier and requiring fewer lines of code. They looked at what real customers were spending their time on with ASP.NET and strove to improve those experiences. For example, probably the most common task an ASP.NET developer finds herself doing is displaying database data. As we'll see later on in this article, the ASP.NET team made this task painfully easy by creating a number of DataSource controls that enable for accessing database data with simple dragging and dropping without having to write a single line of code!

Another major improvement in the area of working with data is the ASP.NET GridView Web control. The GridView is the next version of the DataGrid, one that has common functionality wrapped up, thereby requiring less code and effort from developers wishing to utilize this common functionality. For example, creating a sortable, pageable, editable GridView in ASP.NET 2.0 requires no code, whereas ASP.NET 1.x developers wanting to do the same with a DataGrid must write a decent amount of code.

Another task Web developers oftentimes find themselves faced with is creating a site that has user accounts. This involves a number of steps, such as writing pages to create user accounts, to login, to send a password reminder, and so on. Additionally, once a user has logged on, many sites want to be able to easily reference user-specific data. For example, when logged into your bank's Web site, on the left hand column you might see your account balance, while on the right hand side might be a series of recent transactions you've performed. ASP.NET 2.0 contains a built-in membership API that makes it a cinch to create new user accounts, reset passwords, log on users, load specific user settings, and so on. (You can, in fact, check out the Member Management Component Prototype for ASP.NET 1.x. This free library from Microsoft illustrates a number of the new membership and personalization features that will be present in ASP.NET 2.0.)

There are also a bevy of new Web controls that address commonly created user interface pieces in ASP.NET 1.x Web sites. There's a Login Web control, for example, that saves you the need for creating the UI elements for collecting a user's Username and Password. There's a SiteMap control that shows a user where they are in the Web site's content; a TreeView control; a Menu control; and so on.

This is just a taste of the new features of ASP.NET 2.0. There are dozens of other new features that will, no doubt, warrant their own articles as ASP.NET 2.0's release draws nearer. And if all of these new changes in just ASP.NET weren't enough, Visual Studio .NET has had a major facelift for 2.0. The new version of Visual Studio .NET, called Visual Studio 2005, provides a much improved developer experience. There's IntelliSense in the HTML portions of ASP.NET pages and in Web.config files. Creating a new Web site project doesn't add a gaggle of files to your computer, nor does it even create a new virtual directory in IIS. In fact, Visual Studio 2005 uses its own built-in Web server for testing ASP.NET pages locally, thereby removing IIS as a requirement for developing ASP.NET pages. Visual Studio .NET, in my opinion, seemed like an older version of Visual Studio with .NET support hacked on, and ASP.NET development tacked on as an afterthought. Visual Studio 2005, on the other hand, looks and feels like it was designed from the ground up for .NET development, with prominent time spent on making it ASP.NET developer-friendly.

Working with Data in ASP.NET 2.0

Working with database data in ASP.NET 1.x is relatively easy, although anytime you need to access database code you are required to write a handful of lines of source code. This code, though, is fairly repetitious and, for an experienced ASP.NET 1.x developer, can likely be coded in their sleep. Regardless, accessing a database still requires writing code, and that takes time and provides another avenue for potential bugs and mistakes.

Well, writing code to work with database data is a thing of the past with ASP.NET 2.0 thanks to a new suite of Web controls called the DataSource controls. These controls, as their names imply, are designed to provide control-like functionality for accessing data. That is, to grab data from, say, a SQL Server database, you need only to drag the appropriate DataSource Web control (the SqlDataSource control, in this example) onto the Designer. Upon doing so you can configure the DataSource control through a wizard. You'll be prompted to select the database connection to use and the query or stored procedure to run. The screenshots below show the various screens in the SqlDataSource wizard.

In addition to specifying a SELECT statement you can also use the DataSource to specify INSERT, UPDATE, and DELETE statements to boot. Realize that a DataSource control just provides access to data. In order to actually work with the data, such as displaying said data in the Web page, you need to use an appropriate Web control. For example, the GridView control (ASP.NET 2.0's improved version of the DataGrid) can be used to display, edit, and delete data. To display data, all that you need to do is drag the GridView onto the page and set its DataSource property to the SqlDataSource added in the previous step. That's it! No code necessary!

Accessing Different Types of Data

The SqlDataSource is just one of the many DataSource controls available in ASP.NET 2.0. As its name implies, the SqlDataSource is designed for working with OLE-DB compliant databases, such as Microsoft SQL Server. Additionally there's an AccessDataProvider for working with Access databases, an XmlDataSource for working with XML data, and an ObjectDataSource for working with business objects and data access layer classes that contain the logic for creating, updating, deleting, and accessing data.

The SqlDataSource and AccessDataSource controls are pretty straightforward. Both provide a wizard similar to the one shown in the screenshots above. (The AccessDataSource wizard differs slightly from the SqlDataSource wizard in Step 1; rather than specifying a connection string you are simply prompted to browse to the Access .MDB database file.) The XmlDataSource can intuitively access the entire contents of an XML file or you can specify an XPath expression to work with only a subset of the data. Additionally, the XmlDataSource allows you to optionally specify an XSLT file if you want to message the XML content before working with it.

Accessing data directly from an ASP.NET Web page is usually a poor idea for anything but the most trivial Web applications. Ideally you'll separate your application into different layers: a presentation layer (the ASP.NET pages), a data-access layer (classes designed for working with the underlying data store), and a data layer (the actual data store, the stored procedures, and the data model). The ObjectDataSource allows you to declaratively work with data retrieved from middle-tier objects. With the ObjectDataSource you simply specify the type of the object to use and the method names for selecting, inserting, updating, and deleting data. For more information be sure to read Stephen Walther's article Working with the ASP.NET 2.0 ObjectDataSource Control.


In this article we looked at some of the new features of ASP.NET 2.0, focusing in on the new DataSource controls that make working with data a code-less endeavor. To work with data in ASP.NET 2.0 you simply drag and drop an appropriate DataSource control onto the page and zip through the wizard providing the specifics of accessing the data. Once a DataSource control has been added, you can utilize other Web controls, such as the GridView, to work with the data. All without having to write a lick of source code.

At the time of this writing (December 2004), ASP.NET 2.0 is still in Beta 1, with Beta 2 expected to be released in Q1 2005. You can download the latest versions of the Beta at http://labs.msdn.microsoft.com/.

UPDATE: As of November 2005, ASP.NET 2.0 is out of Beta and officially released. You can learn more about ASP.NET 2.0 at the 2.0 article index here on 4Guys; for more on working with data, check out the Accessing and Updating Data in ASP.NET 2.0 article series.

Happy Programming!

  • By Scott Mitchell

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article