Using the ItemDataBound event with a DataReader vs DataTable/DataView

A little while back I was blogging about how we were switching over to datareaders for getting data back from our database. Well it was working fine until we noticed on some of our pages data wasn't being calculated correctly. This was traced to the ItemDataBound event used in our grids/repeaters not working correctly. With a datatable/view we would cast the e.Item.DataItem as a DataRowView; which works just fine. However datareaders when being databound can't be cast as DataRowViews. You have to use the DbDataRecord class. Afte switching from the DataRowView to DbDataRecord in the ItemDataBound event everything works just like it should. DbDataRecord is in the System.Data.Common namespace. Make sure you are importing that namespace if you haven't already.

So to summarize it:









Binding ObjectWhat to Cast to in ItemDataBound event
DataReaderDbDataRecord
DataTableDataRowView
DataViewDataRowView

Comments

Tim Leung said…
I’ve just encountered the same problem in a project so thanks for the tip.

DbDataRecord belongs in the System.Data.Common namespace for those who haven't imported it.
Justin said…
Thanks for the note Tim. I appreciate the post and am glad it helped. I updated the original blog entry to include the namespace thing.

Popular Posts