[ASP.NET] Problem with DataList - onItemDataBound Method

  • Thread starter Martin Høst Normark
  • Start date
M

Martin Høst Normark

Hi everyone

I get a System.NullReferenceException performing this code:

public void Item_Bound(object sender, DataListItemEventArgs e)
{
if((int)(e.Item.DataItem as DataRowView).Row[11] != 0)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
int aktuelt_a_id = (int)(e.Item.DataItem as DataRowView).Row[11];

if(aktuelt_a_id != temp_a_id)
{
temp_a_id = aktuelt_a_id;
Literal myL = (Literal)e.Item.FindControl("myLiteral");
myL.Text = "<h2>" + aktuelt_a_id + "</h2>";
}
}
}
else
;

}

Even this little opertion throws the same exception:

Double Price =
Convert.ToDouble(((DataRowView)(e.Item.DataItem)).Row.ItemArray[3].ToString());

What might be wrong?
 
M

Marcie Jones

You need to put your ItemType check outside the test of the DataItem, that's what's causing your null reference error.

Marcie
 
M

Martin Høst Normark

Marcie Jones said:
You need to put your ItemType check outside the test of the DataItem,
that's what's causing your null reference error.

Marcie

I've tried to remove the test of the DataItem - and it still throws the same
exception...
 
M

Marcie Jones

Also you shouldn't be referencing row numbers at all, this event fires for each row in the DataList.

Marcie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top