Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys, need help populating my combobox using data from linq

Method to query database

C#
public  bl_Campuses getCampusesList()
    {
        try
        {
            var q = from row in DataAccess.metadata.db_Campus
                    select row;


            var results = new bl_Campuses();
            foreach (var campus in q)
            {
                
                    campusID = campus.campusID;
                    Name = campus.Name;
                   
               
            }

            return results;
        }
        catch (Exception e)
        {
            throw e;
        }
    }



Now stuck on my back code on populating the combo
So i tried this code but placed a breakpoint on the method just to see whatsup
C#
protected  void getCampus()
        {

          bl_Campuses campus = new bl_Campuses();
          var  names = campus.getCampusesList();
          cboLocation.DataSource = names;//error here
         }


Then get the error below:

C#
An invalid data source is being used for cboLocation. A valid data source must implement either IListSource or IEnumerable.
Posted

1 solution

Please change your method like below. You can only bind an Ienumerable collection to combobox
C#
public  bl_Campuses getCampusesList()
    {
        try
        {
            var q = DataAccess.metadata.db_Campus.Select(x=>new bl_Campuses{campusID = x.campusID,
                    Name = x.Name}).ToList();


           return q;
        }
        catch (Exception e)
        {
            throw e;
        }
    }

Hope this helps
 
Share this answer
 
v2

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