Null Exception - DataGridItem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Knight

    Null Exception - DataGridItem

    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
    DataGridItemEve ntArgs)

    'determine if current item is not a header or footer
    If(e.Item.ItemT ype <> ListItemType.He ader Or e.Item.ItemType <>
    ListItemType.Fo oter) Then

    'add index to answer options
    e.Item.Cells(0) .Controls.Add(N ew
    LiteralControl( e.Item.ItemInde x + 1 & "."))

    'determin if logged in user belongs to the director or admin
    groups
    If(Security.IsD irector() Or Security.IsAdmi n()) Then

    Try

    'determine if current option is a correct option
    If(e.Item.DataI tem.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(N ew LiteralControl( "
    Type: " & e.Item.ItemType ))

    End Try

    End If

    End If

    End Sub


  • Mr Newbie

    #2
    Re: Null Exception - DataGridItem

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





    "Adam Knight" <adam@pertrain. com.au> wrote in message
    news:e4S6kJ0vFH A.3588@tk2msftn gp13.phx.gbl...[color=blue]
    > 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
    > DataGridItemEve ntArgs)
    >
    > 'determine if current item is not a header or footer
    > If(e.Item.ItemT ype <> ListItemType.He ader Or e.Item.ItemType <>
    > ListItemType.Fo oter) Then
    >
    > 'add index to answer options
    > e.Item.Cells(0) .Controls.Add(N ew
    > LiteralControl( e.Item.ItemInde x + 1 & "."))
    >
    > 'determin if logged in user belongs to the director or admin
    > groups
    > If(Security.IsD irector() Or Security.IsAdmi n()) Then
    >
    > Try
    >
    > 'determine if current option is a correct option
    > If(e.Item.DataI tem.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(N ew LiteralControl( "
    > Type: " & e.Item.ItemType ))
    >
    > End Try
    >
    > End If
    >
    > End If
    >
    > End Sub
    >
    >[/color]


    Comment

    • Elton W

      #3
      RE: Null Exception - DataGridItem

      You might try condition

      If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
      ListItemType.Al ternatingItem Then

      If(e.Item.ItemT ype <> ListItemType.He ader Or e.Item.ItemType <>[color=blue]
      > ListItemType.Fo oter)[/color]

      might catch some other items

      HTH

      Elton Wang

      "Adam Knight" wrote:
      [color=blue]
      > 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
      > DataGridItemEve ntArgs)
      >
      > 'determine if current item is not a header or footer
      > If(e.Item.ItemT ype <> ListItemType.He ader Or e.Item.ItemType <>
      > ListItemType.Fo oter) Then
      >
      > 'add index to answer options
      > e.Item.Cells(0) .Controls.Add(N ew
      > LiteralControl( e.Item.ItemInde x + 1 & "."))
      >
      > 'determin if logged in user belongs to the director or admin
      > groups
      > If(Security.IsD irector() Or Security.IsAdmi n()) Then
      >
      > Try
      >
      > 'determine if current option is a correct option
      > If(e.Item.DataI tem.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(N ew LiteralControl( "
      > Type: " & e.Item.ItemType ))
      >
      > End Try
      >
      > End If
      >
      > End If
      >
      > End Sub
      >
      >
      >[/color]

      Comment

      Working...