Binding a SqlDataReader to a dropdown list - EntLib 5.0
I'm in the process of updating an app to VS 2010 using EntLib 5. In the app as it stands, there are numerous instances where a dropdown list is bound to a SqlDataReader. However, before it is bound, I use the HasRows property to see if the DataReader is empty. Since the ExecuteReader method in EntLib 5.0 returns an IDataReader, to do the same thing, I need to cast the IDataReader to a RefCountingDataReader and then cast the InnerReader property to a SqlDataReader.
Using rdr As SqlDataReader = CType(CType(db.ExecuteReader("spname"), RefCountingDataReader).InnerReader, S开发者_JS百科qlDataReader)
I found this workaround online and this has been addressed in many places (including SO).
However, another alternative is to load the IDataReader into a DataTable, check the row count and then bind that to the dropdown list. Which is the better option? Or is another option optimal?
Sorry to be so long and drawn out. Any advice would be awesome.
If you use the IDataReader to populate something that is then bound to your drop down list, you will spend an insignificant amount of time doing that conversion, and then whenever you need to look at or debug that code, you will potentially save a significant amount of time trying to figure out what is going on or wrong.
So, while I wouldn't necessarily put it into a DataTable, I would put it into something. I would personally suggest putting it into either an anonymous object or a nested class (one that is only visible in the class that uses it).
精彩评论