

Navigator : Home > Tutorials > Database Tutorials > ...
Displaying Data using ASP.NET 2.0 CheckBoxList and C#
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 moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
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; |
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; } } |
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> |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
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!