0

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

Martin
  • 23,844
  • 55
  • 201
  • 327

2 Answers2

0

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.

OJ.
  • 28,944
  • 5
  • 56
  • 71
  • I don't believe it is Lazy loaded, and i doing the method .LIST which converts it to a LIST so at this point i look at the object via VS 2008 and sure enough everything is in there but in the following format.. – Martin Nov 16 '10 at 06:29
  • @OJ Good analysis, bad solution. Instead of removing lazy load, the right way is to handle the session at the request level. There are many examples around. – Diego Mijelshon Nov 16 '10 at 12:58
  • Diego: I think as a generalisation you can't say it's a bad solution. There are many cases where aggressive loading is a *good* solution. Having said that, I didn't state it was a good solution, just a simple one :) Cheers mate! – OJ. Nov 18 '10 at 10:00
0

Create a List<T> or BindingList<T> object and pass the IList object from the query into the constructor. If the IList object is not a generic list, you can use LINQ, ilistObject.Cast<T>().ToList().

Dmitry S.
  • 8,373
  • 2
  • 39
  • 49