In this article I will explain with an example, how to implement cascading (dependent) ListBox from Database in ASP.Net using C# and VB.Net.
Database
I have made use of the following three tables Countries, States and Cities with the schema as follow.
Countries Table
States Table
Cities Table
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of three ListBoxes each for Country, State and City.
The Country and State ListBoxes have been assigned SelectedIndexChanged event handlers and AutoPostBack property is set to True.
<table>
<tr>
<td>Country:</td>
<td>
<asp:ListBox ID="lstCountries" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="OnCountryChanged"></asp:ListBox>
</td>
</tr>
<tr>
<td>State:</td>
<td>
<asp:ListBox ID="lstStates" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="OnStateChanged"></asp:ListBox>
</td>
</tr>
<tr>
<td>City:</td>
<td>
<asp:ListBox ID="lstCities" runat="server"></asp:ListBox>
</td>
</tr>
</table>
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Generic function to populate ListBox from Database
The following function accepts the reference of the ListBox, the SQL query, the name of the Column to be displayed in Text part and the name of the Column to be displayed in Value part.
Finally, the SQL query is executed and the ListBox is populated from database.
C#
private void BindListBox(ListBox lst, string query, string text, string value)
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
lst.DataSource = cmd.ExecuteReader();
lst.DataTextField = text;
lst.DataValueField = value;
lst.DataBind();
con.Close();
}
}
}
VB.Net
Private Sub BindListBox(lst As ListBox, query As String, text As String, value As String)
Dim conString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
lst.DataSource = cmd.ExecuteReader()
lst.DataTextField = text
lst.DataValueField = value
lst.DataBind()
con.Close()
End Using
End Using
End Sub
Populating the Country ListBox from Database
Inside the Page Load event handler, the Country ListBox is populated from database using the BindListBox generic function.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string query = "SELECT CountryId, CountryName FROM Countries";
this.BindListBox(lstCountries, query, "CountryName", "CountryId");
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim query As String = "SELECT CountryId, CountryName FROM Countries"
Me.BindListBox(lstCountries, query, "CountryName", "CountryId")
End If
End Sub
Populating the State ListBox from Database
Inside the SelectedIndexChanged event handler of the Country ListBox, the selected value of the Country is used to populate the State ListBox from database using the BindListBox generic function.
C#
protected void OnCountryChanged(object sender, EventArgs e)
{
lstStates.Items.Clear();
lstCities.Items.Clear();
int countryId = int.Parse(lstCountries.SelectedItem.Value);
if (countryId > 0)
{
string query = string.Format("SELECT StateId, StateName FROM States WHERE CountryId = {0}", countryId);
this.BindListBox(lstStates, query, "StateName", "StateId");
}
}
VB.Net
Protected Sub OnCountryChanged(sender As Object, e As EventArgs)
lstStates.Items.Clear()
lstCities.Items.Clear()
Dim countryId As Integer = Integer.Parse(lstCountries.SelectedItem.Value)
If countryId > 0 Then
Dim query As String = String.Format("SELECT StateId, StateName FROM States WHERE CountryId = {0}", countryId)
Me.BindListBox(lstStates, query, "StateName", "StateId")
End If
End Sub
Populating the City DropDownList from Database
Inside the SelectedIndexChanged event handler of the State ListBox, the selected value of the State is used to populate the City ListBox from database using the BindListBox generic function.
C#
protected void OnStateChanged(object sender, EventArgs e)
{
lstCities.Items.Clear();
int stateId = int.Parse(lstStates.SelectedItem.Value);
if (stateId > 0)
{
string query = string.Format("SELECT CityId, CityName FROM Cities WHERE StateId = {0}", stateId);
this.BindListBox(lstCities, query, "CityName", "CityId");
}
}
VB.Net
Protected Sub OnStateChanged(sender As Object, e As EventArgs)
lstCities.Items.Clear()
Dim stateId As Integer = Integer.Parse(lstStates.SelectedItem.Value)
If stateId > 0 Then
Dim query As String = String.Format("SELECT CityId, CityName FROM Cities WHERE StateId = {0}", stateId)
Me.BindListBox(lstCities, query, "CityName", "CityId")
End If
End Sub
Screenshot
Demo
Downloads