Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Database - Displaying Data using ASP.NET 2.0 CheckBoxList and C#
Displaying Data using ASP.NET 2.0 CheckBoxList and C#


ASP.NET Database Tutorial

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

The .NET Framework offers a number of classes that makes populating controls with data easy.

We will need to first import the System.Data.SqlClient namespace. The System.Data.SqlClient namespace contains the methods we will need to query our SQL database.

using System.Data.SqlClient;

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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

When the btnSubmit_Click() event fires it queries our database and creates a new SqlDataReader by invoking the ExecuteReader() method of our cmd object. We make sure to specify the DataTextField property so the CheckBoxList control will know which columns to display as a list.

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

SqlDataReader datareader = cmd.ExecuteReader();
chkBoxEx.DataSource = datareader;
chkBoxEx.DataTextField = "firstname";

chkBoxEx.DataBind();

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

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1" style="height: 62px"> Employee Data Populating A CheckBoxList Control:</td>
<td align="center" bgcolor="#FFFFFF" style="height: 62px">
<asp:CheckBoxList ID="chkBoxEx" runat="server">
</asp:CheckBoxList><asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

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

SqlDataReader datareader = cmd.ExecuteReader();
chkBoxEx.DataSource = datareader;
chkBoxEx.DataTextField = "firstname";

chkBoxEx.DataBind();

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

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!



 
  Developer Resources







Server Intellect Rocks