Results 1 to 2 of 2

Thread: Binding combo-box to Entity Data Model

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Location
    N.Ireland
    Posts
    71

    Binding combo-box to Entity Data Model

    Hi,

    I'm trying to keep my data access code separate from the code behind my form. What I am trying to do is populate my combo-box with a list of CompanyNames here's how I'm trying to bind this combo-box to my Companies entity to retrieve the CompanyName:
    C Code:
    1. private void frmMain_Load(object sender, EventArgs e)
    2. {
    3.     dal = new DataAccessLayer();
    4.  
    5.     cmbCompanyNameSearch.DataSource = dal.GetcmbCompanyList();
    6. }

    and here's the method in my DataAccessLayer:
    C Code:
    1. public List<Companies> GetcmbCompanyList()
    2. {
    3.     // Check we have an ObjectContext
    4.     if (entities == null) entities = new CompanySecretaryEntities();
    5.  
    6.         // Create a query from the entityset
    7.     ObjectQuery<Companies> companies = entities.Companies;
    8.     companies.MergeOption = MergeOption.AppendOnly;
    9.  
    10.     // Define the query
    11.     var query = from c in entities.Companies
    12.             select c.CompanyName;
    13.  
    14.     // Execute the query
    15.     List<Companies> results = query.ToList();
    16.  
    17.     // Return the results in a List
    18.     return results;
    19. }
    This is giving me the following error:
    Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<CompanySecretary.Companies>'

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Binding combo-box to Entity Data Model

    The issue is here:
    Code:
    List<Companies> results = query.ToList();
    Your query is getting a single string property for each company, so calling ToList will return a List<string> but you're trying to assign it to a variable of type List<Companies>.

    First things first, you need to decide whether this method is supposed to return the companies or just the names of the companies. If it's the former then you need to change your query to this:
    Code:
    var query = from c in entities.Companies
                select c;
    Note that the 'select' clause gets the whole object and not just one property of the object. You also need to change your binding code to this:
    Code:
    cmbCompanyNameSearch.DisplayMember = "CompanyName";
    cmbCompanyNameSearch.DataSource = dal.GetcmbCompanyList();
    If it's the latter then you need to change the name of the method to reflect that it's getting company names rather than companies and you also need to change the return type to List<string>.

    Finally, what's up with this?
    Code:
    public List<Companies> GetcmbCompanyList()
    Why is there a Hungarian prefix in your data access method? The DAL shouldn't even know that ComboBoxes exist, never mind that the data it returns is destined for one. Further, why are you using Hungarian Notation at all? It's not 1980 any more.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width