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 VB
Displaying Data using ASP.NET 2.0 CheckBoxList and VB

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Database Tutorial

This tutorial will show you how to display data using the .NET CheckBoxList Control, ASP.NET 2.0 and VB.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.

Imports System.Data.SqlClient

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'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 Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", New SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"))

cmd.Connection.Open()

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

chkBoxEx.DataBind()

cmd.Connection.Close()
cmd.Connection.Dispose()
Catch ex As Exception
lblStatus.Text = ex.Message
End Try
End Sub

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

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>

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.

The flow for the code behind page is as follows.

Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", New SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"))

cmd.Connection.Open()

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

chkBoxEx.DataBind()

cmd.Connection.Close()
cmd.Connection.Dispose()
Catch ex As Exception
lblStatus.Text = ex.Message
End Try
End Sub
End Class

Looking for the C#.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