Tutorial RSS
 
Navigator: Home - Display - Display Data using .NET CheckBoxList C#

Display Data using .NET CheckBoxList C#

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


Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!


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

Looking for more DB Tutorials? Click Here!

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;


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;
}
}

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!


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>


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 DB Tutorials? Click Here!

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


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!

411asp.net123aspxDotNetFreaksServer Intellect