ASP.NET
  Home arrow ASP.NET arrow Page 5 - ASP.NET Basics (Part 8): Data Overload
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Basics (Part 8): Data Overload
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 18
    2003-12-01

    Table of Contents:
  • ASP.NET Basics (Part 8): Data Overload
  • Out with the Old...
  • Dumped!
  • Hello Database!
  • Going All the Way
  • Grid Lock

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Basics (Part 8): Data Overload - Going All the Way


    (Page 5 of 6 )

    All working? Good. Now, let's use our new SqlConnection object to do something interesting: fire a SELECT query at the database, and display the results in an HTML page. 


    After the mandatory import of the required ADO.NET assemblies, a connection object is created as explained earlier.

     <%
        
    // create an instance of the Command object
        
    SqlCommand objCommand = new SqlCommand("SELECT * FROM starwars;"objConn);
    %>



    Here, I have created an instance of the SqlCommand object. True to its name, this object allows you to execute commands against the database and possibly return records (in case of a SELECT query). Using this object, you can then navigate through the records in your ASP.NET code.

    The constructor (a method having the same name as that of the class and executed each time an instance is created) of this object requires two parameters: the SQL statement to be executed and the connection object to be used.

     <%
        
    // open the connection
             
    objConn.Open();
             
             
    // populate a SqlDataReader object
             
    SqlDataReader objReader objCommand.ExecuteReader();
    %>



    After opening the connection to the database, an instance of the SqlDataReader object is created. As the name suggests, this object is used to store a read-only set of records and, therefore, can only be used for display purposes. The ExecuteReader() method of the SqlCommand object is then used to populate the SqlDataReader object with the database's response to the command - in this case, a recordset. Note that when using the SqlDataReader object, records can be read in the forward direction only; you cannot access a record once you have moved past it.

    Note also that in this approach, the connection is open throughout the execution of the code and processing of the recordset. This might not seem important at the moment, but remember it because it'll become a big deal soon.

     <%
        
    // display the names of the columns of the "starwars" table
        
    for(int count 0count objReader.FieldCountcount++)
             {
            
    Response.Write("<td><b>" objReader.GetName(count).ToUpper() +
    "</b></td>");
        }
    %>



    The GetName() method of the SqlDataReader object can be used, with a "for" loop, to obtain a list of field names from the recordset, with the FieldCount property exposing the total number of fields.

     <%
        
    // read each record from the resultset and display in the table
             
    while(objReader.Read())
             {
            
    Response.Write("<tr>");
            
    Response.Write("<td>" objReader.GetValue(0) + "</td>");
            
    Response.Write("<td>" objReader.GetValue(1) + "</td>");
            
    Response.Write("<td>" objReader.GetValue(2) + "</td>");
            
    Response.Write("<td>" objReader.GetValue(3) + "</td>");
            
    Response.Write("<td>" objReader.GetValue(4) + "</td>");
            
    Response.Write("<td>" objReader.GetValue(5) + "</td>");
            
    Response.Write("</tr>");
        }
    %>



    Next, the Read() method of the SqlDataReader object can be used to iterate over the resultset and process each record in a "while" loop. All you need to do is pass the field index to the GetValue() method to retrieve the corresponding field value. The "while" loop will terminate automatically as soon as the SqlDataReader object runs out of records.

    Finally, once the entire set of records has been displayed, it's a good idea to free up used memory resources.

     <%
        
    // clear up memory by closing all objects
        
    objReader.Close();
            
    objConn.Close();
    %>



    Since the SqlDataReader object is read-only and can only be used in one direction, it's fast and ensures minimal load on the server - always a good thing in the Web world!

    More ASP.NET Articles
    More By Team Melonfire, (c) Melonfire


     

    ASP.NET ARTICLES

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek