Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Stay Informed
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (29)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Product Reviews (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
Re: Preventing/Investigating the Source of SQ..
by Faisal Khan on 25 Nov 2008 Go To Post

Re: Column multiplication..
by Faisal Khan on 24 Nov 2008 Go To Post

Re: Unable to insert data in an Access databa..
by Faisal Khan on 12 Nov 2008 Go To Post

Re: HOW TO MOVE FROM APACHE TO MS IIS
by Faisal Khan on 12 Nov 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by peeru999 on 8 Oct 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO.NET : 2 Ways of Displaying Data from a SQL Server Database using ASP.NET
 
2 Ways of Displaying Data from a SQL Server Database using ASP.NET
by Faisal Khan.

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
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
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
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
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
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
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
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.


 ( 2 Remaining ) Next

Comments/Questions ( Threads: 9, Comments: 12 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Using VB.NET
  2. different using SQL compare to ODBC
  3. display data from mySQL using asp.net
  4. display data from mySQL using asp.net
  5. hai ( 1 Reply )
  6. Display Data from SQL Server using ASP.NET
  7. inserting record in Access Database?
  8. Displaying Images through Access Database? ( 1 Reply )
  9. N-tier architecture ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserved.