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.
Next: In With The New >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire