Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My listbox contains many values. I am tring to selects those particular values on listbox control which are already save in my database column. My database column has 3 values and when i trying it to show on listbox it selects only last value.
eg:
ASP.NET
<asp:ListBox ID="ListBox1" runat="server" Height="149px" SelectionMode="Multiple" Width="113px">
        <asp:ListItem Value="1">Vinz</asp:ListItem>
        <asp:ListItem Value="2">Jhen</asp:ListItem>
        <asp:ListItem Value="3">Chris</asp:ListItem>
        <asp:ListItem Value="4">Shynne</asp:ListItem>
        <asp:ListItem Value="5">Chu</asp:ListItem>
        <asp:ListItem Value="6">Mark</asp:ListItem>
        </asp:ListBox> 

C#
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
      {
         ListBox1.SelectedValue = reader["subcategory_id"].ToString();
      }

SQL
select r.*, sc.subcategory_id from dbo.tbl_adregister r INNER JOIN tbl_adsubcategory sc on r.ad_id=sc.ad_id	

Table (tbl_adsubcategory) has 1,2,3 in its column (subcategory_id) but only 3 is show selected in the listbox, i want that all 3 is select in the listbox.
Hope you able to understand my problem. Plz help.
Posted
Updated 9-Jun-14 8:53am
v2

here's a clue...

C#
SqlDataReader reader = cmd.ExecuteReader();
List<string> selected = new List<string>();

while (reader.Read())
{
     selected.Add(reader["subcategory_id"].ToString());
}

listView1.SelectedItems.Clear();
for(int i=0;i<selected.count();i++)>
        listView1.Items[(int)i].Selected = true;

</string></string>
 
Share this answer
 
Comments
Raj Negi 9-Jun-14 15:32pm    
last two close string gives error
goathik 10-Jun-14 7:35am    
I'm sorry, I'm not used to this forum text formating. ignore those
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900