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