Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
ASP.NET Basics (part 9): Different Strokes
Find out how to view, add and edit records in a database with ADO.NET.

| 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>
<html>
<head></head>
<body>
<asp:datagrid id="starwars" runat="server" />
</body>
</html>


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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
<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:

Output image

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.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback