---
title: "Populate Records in Second Listbox According to the Selection in First List box"  
description: "Suppose you have two list box, ListBox1 and ListBox2 and you want to populate values of ListBox2 according to the selection in ListBox1 then it’s very"  
author: "Anonymous User"  
published: 2010-11-29  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Populate Records in Second Listbox According to the Selection in First List box

Suppose you have two list box, ListBox1 and ListBox2 and you want to [populate](https://www.mindstick.com/forum/1781/how-to-populate-a-list-inside-a-dictionary) [values](https://www.mindstick.com/forum/327/sum-textbox-values) of ListBox2 according to the [selection](https://www.mindstick.com/blog/23186/importance-of-selection-of-topic-for-research-and-thesis) in ListBox1 then it’s very [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) you can just use to find the value of your selected item of you ListBox1 and [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) your query accordingly.\

But, what if ListBox1 is multiselect list box, i.e. SelectionMode="[Multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors)" of ListBox1. Then in that case you will have to display all the [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) related to all the [selected items](https://www.mindstick.com/forum/352/i-need-to-display-my-checkboxlist-selected-items-in-a-label-how-to-do) in the first list box.

Now, here I am going to show how we can accomplish this.

I have two list boxes, ListBox1 and ListBox2 and they both are bound with [Data source](https://www.mindstick.com/interview/808/what-is-a-data-source).

Create statement for the [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development) used in this example.

```
create table tblSubCat (SubCatID int, SubCat varchar(50), CategoryID int) create table tblCategory (CategoryID int, Category varchar(50))
```

##### Here is the aspx code for the file.

```
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" DataSourceID="SqlDataSource1" DataTextField="Category" DataValueField="CategoryID"></asp:ListBox>  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="SELECT * FROM [tblCategory]"></asp:SqlDataSource>         <asp:ListBox ID="ListBox2" runat="server" DataSourceID="SqlDataSource2" DataTextField="SubCat" DataValueField="SubCatID"></asp:ListBox>         <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"></asp:SqlDataSource>         <asp:Button ID="Button1" runat="server" Text="Populate" OnClick="Button1_Click" />
```

According to the code above ListBox1 will be populated on page.

Now what we want to do is to populate the second list box according to the selections in ListBox1 if the selection is multiple, so in order to accomplish our goal we have to write some logic to find all the selected items in ListBox1.

##### Here is the code to accomplish the above goal.

```
protected void Button1_Click(object sender, EventArgs e)     {         string query = "";         for (int i = 0; i < ListBox1.Items.Count; i++)         {             if (ListBox1.Items[i].Selected == true)             {                 if (query.Length > 0)                     query = query + " or ";                 query = query + " CategoryID=" + ListBox1.Items[i].Value;             }         }         if (query.Length > 0)             SqlDataSource2.SelectCommand = "Select * from tblSubCat where " + query;     }
```

---

Original Source: https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
