Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want the selected index change event of a combo box to read data from my data table and
fill a listbox with the result. my code does not seem to work. any help will be much appreciated.
The code is detailed below:

C#
if (mgDept.SelectedItem != null)
{
    searchresults rslts = mgDept.SelectedItem as searchresults;
    if (rslts != null)
    {
        id = rslts.fileloc;
    }
}
exp = "dept_id = '" + id + "'";
DataRow[] foundrows = cvmanagerDataSet1.Tables["role"].Select(exp);
for (int i = 0; i <= foundrows.Length; i++)
{
    roleid = foundrows[i][0].ToString();
    name = foundrows[i][2].ToString();
    mgRoles.Items.Add(new searchresults(name, roleid));
}
Posted
Comments
shakil0304003 25-Jan-11 6:17am    
not clear, your problem!!!
[no name] 25-Jan-11 6:30am    
i want to fill a listbox from a combo box. when the user selects an entry in the combo box it should use that entry's id to pull out related info from another table and display in the listbox
wizardzz 25-Jan-11 11:33am    
Please clarify why it doesn't work? An error? mgRoles contains no items? What does it look like when you debug the loop?

Get the value form Combo box index change event and pass the value of Query. U get values
 
Share this answer
 
I think you are on the right track using the .Select for the datatable...it's often faster than hitting the database and with the additional WHERE portion of the query.

You may want to check on the data...if you are comparing a string value you have to match the case...if your variable id has a value "Bob" and your file has "BOB" it won't match and return values you are expecting...so you may have to manipulate the case and/or trim it before doing the .Select.

If that doesn't work, could you please explain what you mean by saying it isn't working? Are you getting any values to add into mgRoles? Are they the wrong values?
 
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