I'm trying to get a label inside a repeater in a "for" loop, but I keep getting an error saying:
"An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code
Additional information: Index was out of range. Must be non-negative and less than the size of the collection."
Here's my code:
for (var i = 0; i < dt.Rows.Count; i++)
{
Label AppAmmount = (Label)rpOffers.Items[i].FindControl("AppAmmount");
}
You are looping dt.Rows.Count
but you are accessing rpOffers.Items
. It seems the DataTable
contains more rows than the repeater.
But why not a simple foreach
?
foreach(RepeaterItem item in rpOffers.Items)
{
Label AppAmmount = (Label)item.FindControl("AppAmmount");
}