ASP.NET
  Home arrow ASP.NET arrow Page 2 - ASP.NET Basics (Part 9): Different Strokes
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 
Moblin 
JMSL Numerical Library 
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 9): Different Strokes
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2003-12-23

    Table of Contents:
  • ASP.NET Basics (Part 9): Different Strokes
  • The XML Files
  • In With The New
  • Winds of Change
  • Endgame

  • 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 9): Different Strokes - The XML Files


    (Page 2 of 5 )

    The XML Files

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

        
    // the SQL query
        
    string strSQL "SELECT * FROM starwars";
        
        
    // create an SqlDataAdapter object
        
    SqlDataAdapter objAdapter =new SqlDataAdapter(strSQL,objConn);
        
        
    // create a new DataSet object
        
    DataSet objDataSet = new DataSet();
        
        
    // populate the DataSet object
        
    objAdapter.Fill(objDataSet"starwars");
        
        
    // bind the DataGrid to the DataSet 
        // this will populate the DataGrid automatically
        
    starwars.DataSource objDataSet;
        
    starwars.DataBind();
        
            
    // clear up memory by closing all objects
            
    objDataSet.Clear();
            
    objConn.Close();    
    }
    </SCRIPT>
    <asp:datagrid id=starwars runat="server"></asp:datagrid>



    The output is nothing to write home about - all the records from the "starwars" table are displayed in a neat table - but the details make for interesting reading.

    Once the required namespaces have been imported into the script, it's time to start creating some objects.

     <%
        
    // create an instance of the SqlConnection object
        
    SqlConnection objConn = new SqlConnection(strConn);

        
    // the SQL query
        
    string strSQL "SELECT * FROM starwars";
        
        
    // create an SqlDataAdapter object
        
    SqlDataAdapter objAdapter =new SqlDataAdapter(strSQL,objConn);
    %>



    The SqlConnection object sets the ball rolling. It's followed by the new SqlDataAdapter object, which acts as the connection between the database and the user interface; this object requires an SQL SELECT query and the newly-created SqlConnection object as constructor arguments. I've provided both in the example above.

    Once the SqlDataAdapter is in place, the next step is to associate it with a DataSet object and then bind it to a user interface (in this example, a ASP.NET DataGrid control).

     <%
        
    // create a new DataSet object
        
    DataSet objDataSet = new DataSet();
        
        
    // populate the DataSet object
        
    objAdapter.Fill(objDataSet"starwars");
    %>



    Here, I've used the Fill() method of the SqlDataAdapter object to populate the DataSet object. The second parameter to Fill() specifies the name for the DataTable which will store the information within the DataSet.

    Wanna take a quick peek inside the newly-populated DataSet object? It's easy; just use the GetXML() method of the DataSet object, like this:

     <%    
        
    // populate the DataSet object
        
    objAdapter.Fill(objDataSet"starwars");
        
        
    // output the data in XML format
        
    Response.Write(objDataSet.GetXml());
    %>



    and you should see something like this:

     
    <NEWDATASET><STARWARS><ID>1</ID> <NAME>Darth Maul</NAME
    <
    HOMEWORLD>Iridonia</HOMEWORLD> <SPECIES>Zabrak</SPECIES> <GENDER>Male</GENDER
    <
    AFFILIATION>Sith</AFFILIATION> </STARWARS><STARWARS><ID>2</ID> <NAME>Obi-Wan 
    Kenobi 
    </NAME><HOMEWORLD>NA</HOMEWORLD> <SPECIES>Human</SPECIES
    <
    GENDER>Male</GENDER> <AFFILIATION>Jedi</AFFILIATION
    </
    STARWARS><STARWARS><ID>3</ID> <NAME>Qui-Gon Jinn</NAME
    <
    HOMEWORLD>NA</HOMEWORLD> <SPECIES>Human</SPECIES> <GENDER>Male</GENDER
    <
    AFFILIATION>Jedi</AFFILIATION> </STARWARS><STARWARS><ID>4</ID> <NAME>C-3PO 
    </NAME><HOMEWORLD>Tatooine</HOMEWORLD> <SPECIES>Droid</SPECIES
    <
    GENDER>NA</GENDER> <AFFILIATION>Rebel Alliance</AFFILIATION
    </
    STARWARS><STARWARS><ID>5</ID> <NAME>Luke Skywalker</NAME
    <
    HOMEWORLD>Tatooine</HOMEWORLD> <SPECIES>Human</SPECIES> <GENDER>Male</GENDER
    <
    AFFILIATION>Jedi</AFFILIATION> </STARWARS><STARWARS><ID>6</ID> <NAME>Darth 
    Vader
    </NAME> <HOMEWORLD>Tatooine </HOMEWORLD><SPECIES>Human</SPECIES
    <
    GENDER>Male</GENDER> <AFFILIATION>Empire</AFFILIATION
    </
    STARWARS></NEWDATASET>



    OK, back to business. At this point, I have populated the DataSet object, but still have an empty "starwars" DataGrid to populate. How do I do that?

     <%
        
    // bind the DataGrid to the DataSet 
        // this will populate the DataGrid automatically
        
    starwars.DataSource objDataSet;
        
    starwars.DataBind();
    %>



    As shown above, all I need to do is set the newly populated DataSet object as the data source for the "starwars" DataGrid. The DataBind() method of the DataSet object takes care of the rest. Here's the output:



    You saw something like this in the previous segment of this tutorial also. But before you say "been there, done that" to the script above, I should tell you that I've taken you through the above example again as a prelude to actually manipulating the database. Keep reading, that's coming up next.

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - 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
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...





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