Overview
In this tutorial we will learn what are the two common ways of displaying data from an SQL Server database
using ADO.NET in an ASP.NET page. Following are the topics covered in this tutorial :
- How to create a database in SQL Server?
- How to create a table in SQL Server?
- How to insert data into that table?
- How to create an ASP.NET page?
- What are SqlConnection, SqlCommand, SqlDataReader classes?
- How to use above classes to display data from SQL Server database?
- How to display data using ASP 3.0 style SqlDataReader class?
- What are ASP.NET Web Form controls?
- How to display data using ASP.NET Web Form controls?
- What are the advantages/disadvantages of using one way of displaying data over the other?
- Is there any performance difference between these two methods?
You'll be able to see the online demo on the last page and will also be able to download complete
source code from there. But I recommend that you follow this tutorial step-by-step and create the
pages, tables etc as required instead of waiting for the last page. If you follow this article carefully
and pay due attention, I assure you that you will be able to successfully connect and display data from
your SQL Server database. Once you complete this article you'll be able to move on to more advanced topics,
but completing this tutorial is a must.
I am assuming that this is your first interaction with ASP.NET as far as accessing data from a database
is concerned, that's why I'll try to explain everything in detail.
I. Creating the desired database in SQL Server :
A database provides persistence to your data, you tell the database what kind of data you
want to store, how large should it be? and who should be able to access it. All the gory details
are handled by your database server. For this tutorial's sake we'll be focusing ourselves on an
enterprise-class database system from Microsoft, SQL Server 2000.
I'll assume that you've installed SQL Server correctly and that it has been started, if not
start it now. Let's go to Start -> Programs -> Microsoft SQL Server -> Enterprise Manager. You'll
see something like following :
SQL Server - Enterprise Manager
Expand 'Microsoft SQL Servers' node, you'll now see 'SQL Server Group' ( i.e. if you haven't changed
anything ). Expanding this group will show your 'yourcomputername' ( substitute 'yourcomputername' with
the name of your computer, it is the same as 'localhost' ) node. Expand this node to see something like
following ( since name of my computer is 'MAIN', it is showing 'MAIN' ) :
SQL Server - Enterprise Manager
Right-click over 'Databases' node and choose 'New Database'. In the 'Database Properties' windows that
appears, enter the name 'Stardeveloper'. All the tables that you create for tutorials on Stardeveloper
should be created in this database.
SQL Server - Database Properties Window
After doing that hit 'ok' and now expand the 'Databases' node, you should see your 'Stardeveloper'
database there.
II. Creating the desired 'Names' table in 'Stardeveloper' database :
Now expand the 'Stardeveloper' node from the node/s under the 'Databases' node. Right-click over 'Tables'
icon and from the options choose 'New Table'.
SQL Server - Creating New Table
In the 'New Table' design windows, add three columns whose names, data types and length is given
below :
Creating New Table
Set 'ID' to primary key and identity to 'Yes' ( see above picture ). Now hit the 'save' button and give this
table a name of 'Names', yes that's right. This table will hold first and second names of people. Once you've done
that, your table 'Names' will be created for you in 'Stardeveloper' database.
III. Populating the 'Names' table
Let's insert some data into our 'Names' table that we just created. Select 'Tables' icon under the 'Stardeveloper'
database icon. You should see 'Names' table in the list of tables there. Now right click 'Names' table and from the options
choose 'Open Table' and then from the options which pop-up choose 'Return all rows'.
Inserting Data into 'Names' Table
From the windows that opens, insert first and last names for 3-4 times. Remember, don't try to insert any value
in the 'ID' column, that column will get a value on it's own, just enter values in 'FirstName' and 'LastName' columns as
shown below :
Inserting Data
Now we are done as far as our database is concerned. We have our data, a list of first and last names in a table
called 'Names' which is in a database 'Stardeveloper'. We are now all set to display this data, we can display this data using
any number of technologies e.g. DBI, PHP, Java Servlets, ASP/ASP.NET. For this tutorial's sake we'll use ASP.NET pages to display this
data.
IV. First Way of Displaying Data from the Database ( Classic ASP )
I'll assume here that you've installed .NET Framework from
www.asp.net and are using IIS 5.0 on Windows NT/2000/XP/. Now the first way of displaying data is a way most of you will be
pretty much used to. This is the way data is displayed in DBI, PHP, Java Servlets and ASP 3.0.
Here you first connect to the database, execute a query which returns results, iterate through records one by one and
continue to display those records during the iteration, once all the records have been displayed, close the connection and any
allotted resources.
For ASP.NET pages to work you have to create an application under IIS first. For that first create a sub-folder under your
%DOCUMENT_ROOT%. Now this %DOCUMENT_ROOT% can be C:\Inetpub\wwwroot under default installation, whatever the %DOCUMENT_ROOT% is on
your system, go to that folder and create a sub-folder with name of 'NET' ( note: this can be any name but for tutorial's sake I'll
stick with 'NET' as the name of that folder/application ). We have created the folder but haven't yet made it an application.
To make our %DOCUMENT_ROOT%/NET folder an application go to Start -> Settings -> Control Panel -> Administrative Tools -> Internet
Services Manager. Note the exact might be different on your system, the path that I've given is on Windows 2000.