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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





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