Getting the data bound to a Repeater

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I am attempting to get the data that was bound to an item in a Repeater
control for use in the ItemDataBound event. Here is the code I am using to
attempt to retrieve the data:

CType(e.Item.DataItem, DataRowView)("fieldname")

This has normally worked for me in the past, but once in a while it gives me
a problem for reasons I cannot understand. The error I am recieving is:

Object reference not set to an instance of an object.

I know that the datatable that I am binding to the Repeater has data in it,
because the data is displayed on the page if I comment out this line of the
code. Does anybody have any idea what I am doing wrong? Thanks.
 
It might not be the correct item type because of header templates etc., e.g.
(C#):

if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// FindControl code here
}
 
Hi Nathan,


Before you access assiociated DataItem check the current item's type:
If e.Item.ItemType = ListItemType.Item or _
e.Item.ItemType = ListItemType.Item then

CType(e.Item.DataItem, DataRowView)("fieldname")

end if

hope it helps
 
Back
Top