Search code examples
asp.netnhibernatedata-bindingilistasp.net-controls

NHibernate and ASP.NET Binding to a IList of <Products>?


I have recently started to use Nhibernate and i am quite happy with it until i needed to BIND to ASP.NET controls. I was having major issues binding a gridview to a collection of Products (IList). In the end i was forced to right a small routine to convert my IList to a DataTable. Once it was in datatable it worked flawlessy.

Now has come the time to bind a standard Dropdownbox to 1 field of a collection (IList) of Products but it appears i am having issues again.

So this has brought me to the conclusion that i must be doing something wrong?

I can't believe that it isn't possible to BIND ASP.NET controls to a collection (IList) of a class (in my case products) that is returned from NHibernate.

I would really appreciate any feedback anyone has on the situation... I am at a loss

Thank you


Solution

  • The problem is not that you can't bind, because you can. Generally issues like this come about when you're binding at the wrong time.

    NHibernate supports laziness. So if your query is lazy, and properties on the returned objects are lazy, then the values won't be pulled from the database until the items and properties are referenced. If you bind these to controls in the UI, then the values won't be extracted until the page gets rendered.

    At this point there is a good chance that you have already closed your database connection.

    The simple solution is to make sure that the data you're binding to is not lazily loaded.