DotNet Tutorials

Server Intellect

 Displaying Data using ASP.NET 2.0 DataList and C#

This tutorial will show you how to display data using the .NET DataList Control, ASP.NET 2.0 and C#.NET

The DataList control is an easy to use tool similiar to the Repeater control.

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

First, you will need to import the System.Data.SqlClient namespace.

The System.Data.SqlClient namespace contains the SqlCommand and SqlConnection classes that we need in order to connect to our database and to send an SQL command to it.
using System.Data.SqlClient;

We'll put our code in the Page_Load() event.

When the Page_Load() event fires, a new SqlCommand object is instantiated with our connection string and our command.

Afterwards, we will attempt to connect using the Open() method of our cmd.Connection object. Once it is connected we will attempt to execute the command we specified earlier (in this example "SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES" in the Northwind db).

If all goes well, we will have the results of our SQL query assigned to the dlExample's DataSource property. Now all we have to do is call the DataBind() method of our dlExample to bind the data to the control. The data is now ready to be displayed.

protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));

cmd.Connection.Open();

dlExample.DataSource = cmd.ExecuteReader();

dlExample.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

We have to add a few tags on the front end of the .aspx page to place where we want the DataList control to display its bound data. Inside of the main DataList tag, we use the <HeaderTemplate> tag to specify the column titles for our repeated data and the <ItemTemplate> tag to specify what HTML tags can go around that repeated data (in this case, <td> tags surround each item). So, the front end .aspx page looks something like this:

<td align="center" bgcolor="#FFFFFF">
<asp:DataList ID="dlExample" runat="server" CssClass="basix" GridLines="Both">
<HeaderTemplate>
<td>First Name</td>
<td>Last Name</td>
<td>Hire Date</td>
</HeaderTemplate>
<ItemTemplate>
<td><%# DataBinder.Eval(Container.DataItem,"firstname") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"lastname") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"hiredate") %></td>
</ItemTemplate>
</asp:DataList>
<asp:label ID="lblStatus" runat="server"></asp:label></td>

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));

try
{
cmd.Connection.Open();

dlExample.DataSource = cmd.ExecuteReader();

dlExample.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!



Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect