Null Exception - DataGridItem

A

Adam Knight

Hi all,

The following code works fine.

If i remove the try statement, i recieve a generic is null exception when
trying to retrieve a datagrids data item.

With the try those exceptions are obviously caught, but it is clear the data
item is accessible otherwise as i am able to use its value.

I am just wondering (without the try statement) what the function could be
encountering to set of the null exception error.

Any thoughts?

Adam

Sub dgAnswerOptions_ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs)

'determine if current item is not a header or footer
If(e.Item.ItemType <> ListItemType.Header Or e.Item.ItemType <>
ListItemType.Footer) Then

'add index to answer options
e.Item.Cells(0).Controls.Add(New
LiteralControl(e.Item.ItemIndex + 1 & "."))

'determin if logged in user belongs to the director or admin
groups
If(Security.IsDirector() Or Security.IsAdmin()) Then

Try

'determine if current option is a correct option
If(e.Item.DataItem.Item(2) = 1) Then //error occurs
here without try

'change cell formatting to reflect a correct
answer
e.Item.Cells(1).CssClass = "correct"
e.Item.Cells(1).Text = e.Item.Cells(1).Text & "
(Correct)"

End If

Catch

e.Item.Cells(1).Controls.Add(New LiteralControl("
Type: " & e.Item.ItemType))

End Try

End If

End If

End Sub
 
M

Mr Newbie

The problem I think is that the data item may be null yet you are testing it
against nothing first
 
G

Guest

You might try condition

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

If(e.Item.ItemType said:
ListItemType.Footer)

might catch some other items

HTH

Elton Wang
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top