ASP.NET
  Home arrow ASP.NET arrow Page 6 - 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 - Grid Lock


    (Page 6 of 6 )

    Now, that was a long and complex example, loaded with a large number of objects. Take a breather, and let me show you a simpler (though less flexible) way to accomplish the same thing using the DataGrid server control. As the name suggests, this control allows you to display a database result set in a neat little grid without needing too much code. Here's an example:

     <%@ Import Namespace="System.Data"%><%@ Import Namespace="System.Data.SqlClient"%>
    <
    SCRIPT language=C# runat="server">
    void Page_Load()
    {    

        
    // build the connection string
        
    string strConn "user id=john;password=secret;";
        
    strConn += "initial catalog=pubs;data source=tatooine;";

        
    // create an instance of the SqlConnection object
        
    SqlConnection objConn = new SqlConnection(strConn);
        
        
    // create an instance of the Command object
        
    SqlCommand objCommand = new SqlCommand("SELECT * FROM starwars;"objConn);
        
        
    // open the connetion
             
    objConn.Open();
             
             
    // populate a SqlDataReader object
            
    SqlDataReader objReader objCommand.ExecuteReader();

        
    // set the source of the "starwars" datagrid
             
    starwars.DataSource objReader;
             
             
    // bind the data to the grid
             
    starwars.DataBind();
             
            
    // clear up memory by closing all objects
        
    objReader.Close();
            
    objConn.Close();
            
    }
    </SCRIPT>
    <asp:datagrid id=starwars runat="server"></asp:datagrid>



    And this is the output.



    I'm sure that you will not fail to notice the drastic reduction in the amount of code I've written; in one flawless maneuver, I have removed all the complicated "for" and "while" loops of the earlier example. Most of this is due to the DataGrid server control, which takes care of converting the resultset into an HTML table.

    The first step is to define the server control, as below:

     <asp:datagrid id=starwars runat="server"></asp:datagrid>



    As before, after creating the mandatory connection object, I have used the SqlCommand and SqlReader objects to retrieve the results of the SELECT query from the database. However, this time round, I have not bothered iterating through the resultset. Instead, I've used the "DataSource" property of the server control to point it to a populated SqlDataReader object that serves as the source for the data to be displayed in the grid.

     <%
        
    // populate a SqlDataReader object
            
    SqlDataReader objReader objCommand.ExecuteReader();

        
    // set the source of the "starwars" datagrid
             
    starwars.DataSource objReader;
             
             
    // bind the data to the grid
             
    starwars.DataBind();
    %>



    The DataBind() method does the rest, binding the data to the individual elements of the grid and generating the output shown above. Simple, huh?

    And that's about it for today! In this article, I gave you a quick introduction to ADO.NET and how it differs vastly from its predecessor, together with an example of how to get a connection up and running between an ASP.NET page and a SQL Server 2000 RDBMS. This was followed with another example explaining the objects used to retrieve and iterate over a recordset from the database, and an explanation of the ASP.NET DataGrid server control for quick-and-dirty data display.

    What's cooking for next time, you ask? Lots, say I: first, an introduction to more objects of the ADO.NET family, including the DataSet object for disconnected data access. So make sure you don't miss the next episode of this tutorial!

    Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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