views:

160

answers:

3

There is two classes

1 class A[properties:-aid,aname]

2 class B[properties:-bid,A,bname]

DropDownList ddlist;

ICriteria criteria = ActiveRecordMediator<B>.GetSessionFactoryHolder()
                     .CreateSession(typeof(B)).CreateCriteria(typeof(B))
                     .setFetchMode(“A”,FetchMode.JOIN);

ddlistToLet.DataSource = criteria.List();

ddlistToLet.DataTextField = "bname";

ddlistToLet.DataValueField = "aid";

ddlistToLet.DataBind();

I get this error

DataBinding: 'B' does not contain a property with the name 'aid'.

How solve this problem?

A: 

When using criteria, NHibernate will return a 2 dimensional array of results. I'm thinking that the drop down cannot get a sense of what the underlying object structure is. Take a look at the results and you should be able to adjust your binding.

Adam Fyles
A: 

Provide a property with the value.

class B
{
  public A A { get; set;}
  public aid { get { return A.aid; } }
}

If you don't like it, because you don't want to change your class, then you can use a special class to bind to the control.

Stefan Steinegger
A: 

Transform the results of your query to a more "binding-friendly" object:

public class MyDTO {
  public string BeeName { get; set; }
  public string AId { get; set; }
}

// .. (snip) ..

ICriteria criteria = ActiveRecordMediator<B>.GetSessionFactoryHolder()
  .CreateSession(typeof(B)).CreateCriteria(typeof(B))
  .CreateCriteria(“A”)
  .SetProjection(Projections.ProjectionList()
    .Add(Projections.Property("bname", "BeeName"))
    .Add(Projections.Property("aid", "AId"))
  )
  .SetResultTransformer(Transformers.AliasToBean(typeof(MyDTO)));

IList<MyDTO> results = criteria.List<MyDTO>();
Fábio Batista