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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT