Binding columns of related table to listbox datatextfield

Posts   
 
    
w3stfa11
User
Posts: 13
Joined: 01-Jul-2010
# Posted on: 10-Aug-2010 22:58:00   

Here's the snippet


            EmployeeCollection employees = new EmployeeCollection();
            RelationCollection relations = new RelationCollection();
            relations.Add(EmployeeEntity.Relations.UserEntityUsingEmployeeId);
            employees.GetMulti(null, relations);

            lstInvestigators.DataSource = employees;
            lstInvestigators.DataValueField = EmployeeFields.Id.Name;
  -->    lstInvestigators.DataTextField = "?????"  // "User.FirstName"?
            lstInvestigators.DataBind();

It's not finding the FirstName column on the User table. Is there another way of doing this?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 10-Aug-2010 23:07:36   

Yep, you need to fetch the related users using a PreFetch path, not a Relation. Relations are used to add related tables into the query used to fetch the entity (ie to query on a field not in the main entity table), but they do not fetch related entities.

Prefetch paths are used to fetch related entities - ie the Users in your example.

See the documentation at

http://www.llblgen.com/documentation/2.6/Using%20the%20generated%20code/SelfServicing/gencode_prefetchpaths.htm

Matt

w3stfa11
User
Posts: 13
Joined: 01-Jul-2010
# Posted on: 10-Aug-2010 23:52:34   

Thanks for the explanation, Matt. If I understand correctly, I can remove the relationcollection in this case.

EmployeeCollection employees = new EmployeeCollection();

            PrefetchPath prefetch = new PrefetchPath(EntityType.EmployeeEntity);
            prefetch.Add(EmployeeEntity.PrefetchPathUser);
            employees.GetMulti(null, prefetch);

            lstInvestigators.DataSource = employees;
            lstInvestigators.DataValueField = EmployeeFields.Id.Name;
            lstInvestigators.DataTextField = "User.FirstName";
            lstInvestigators.DataBind();  

I'm getting this error message. DataBinding: 'NWMG.ORM.EntityClasses.EmployeeEntity' does not contain a property with the name 'FirstName'.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Aug-2010 03:15:27   

Oh I see. To be able to bind to that related element you need to create a custom property on your EmployeeEntity:

public partial class EmployeeEntity
{
     public string UserName
     {
            get
            {
                  string toReturn = string.Empty;
                  if (User != null)
                  {
                        toReturn = User.FisrtName;
                  }
            }
     }
}

You can do that in a partial class on at EmployeeEntity in USER_CODE_REGIONS. (more info about adding your own code to entity classes).

Then in your fetch/binding code:

EmployeeCollection employees = new EmployeeCollection();

PrefetchPath prefetch = new PrefetchPath(EntityType.EmployeeEntity);
prefetch.Add(EmployeeEntity.PrefetchPathUser);
employees.GetMulti(null, prefetch);

lstInvestigators.DataSource = employees;
lstInvestigators.DataValueField = EmployeeFields.Id.Name;
lstInvestigators.DataTextField = "UserName";
lstInvestigators.DataBind();
David Elizondo | LLBLGen Support Team
w3stfa11
User
Posts: 13
Joined: 01-Jul-2010
# Posted on: 12-Aug-2010 18:47:21   

Ah, thanks! Much easier than I thought.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 12-Aug-2010 20:43:11   

Most things turn out to be once you get them sorted in your head simple_smile

Matt