Weird Null reference error problem

Discussion in 'ASP .Net' started by MattB, Oct 16, 2007.

  1. MattB

    MattB Guest

    I'm relatively familiar with asp.net and vb.net and have been working in
    the environment for several years. I've dealt with null reference errors
    before and generally know how to code around them
    But now I have some new code I deployed and I'm getting a null reference
    error when looping through a datatable (for each r in dt.rows...).
    I'm expecting to find an integer in this field, or possibly nothing, or
    possibly a dash (-).
    So I added some extra stuff to figure out what's in there (I don;t have
    access to the same dataset where this is running, or I'd just use the
    debugger). I made this block of code to help me figure it out:

    If IsNumeric(Trim(r("qty_rem"))) Then 'Trim(r("qty_rem")) <> "-" And
    Trim(r("qty_rem")) <> "" Then
    Dim iRem As Int16
    Try
    iRem = Convert.ToInt16(Trim(r("qty_rem")))
    Catch ex As Exception
    If Not IsNothing(r("qty_rem")) Then
    Current.Session("lastError") = "r(qty_rem) = '" & r("qty_rem") & "'"
    & vbCrLf & ex.Message & vbCrLf & ex.InnerException.Message
    Current.Response.Redirect("error.aspx")
    Else
    Current.Session("lastError") = "r(qty_rem) is nothing" & vbCrLf &
    ex.Message & vbCrLf & ex.InnerException.Message
    Current.Response.Redirect("error.aspx")
    End If
    End Try

    And now instead of redirecting to my error page and showing me what is
    in there, it crashes on the line where it tries to assign values to the
    "lastError" session variable. But it passes If not isNothing() and
    IsNumeric(). I don't get it. Can anyone see the problem in finding out
    what is in this dataset? Thanks!

    Matt
    MattB, Oct 16, 2007
    #1
    1. Advertising

  2. MattB

    IfThenElse Guest

    try testing if r("qty_rem") is nothing first


    "MattB" <> wrote in message
    news:...
    > I'm relatively familiar with asp.net and vb.net and have been working in
    > the environment for several years. I've dealt with null reference errors
    > before and generally know how to code around them
    > But now I have some new code I deployed and I'm getting a null reference
    > error when looping through a datatable (for each r in dt.rows...).
    > I'm expecting to find an integer in this field, or possibly nothing, or
    > possibly a dash (-).
    > So I added some extra stuff to figure out what's in there (I don;t have
    > access to the same dataset where this is running, or I'd just use the
    > debugger). I made this block of code to help me figure it out:
    >
    > If IsNumeric(Trim(r("qty_rem"))) Then 'Trim(r("qty_rem")) <> "-" And
    > Trim(r("qty_rem")) <> "" Then
    > Dim iRem As Int16
    > Try
    > iRem = Convert.ToInt16(Trim(r("qty_rem")))
    > Catch ex As Exception
    > If Not IsNothing(r("qty_rem")) Then
    > Current.Session("lastError") = "r(qty_rem) = '" & r("qty_rem") & "'" &
    > vbCrLf & ex.Message & vbCrLf & ex.InnerException.Message
    > Current.Response.Redirect("error.aspx")
    > Else
    > Current.Session("lastError") = "r(qty_rem) is nothing" & vbCrLf &
    > ex.Message & vbCrLf & ex.InnerException.Message
    > Current.Response.Redirect("error.aspx")
    > End If
    > End Try
    >
    > And now instead of redirecting to my error page and showing me what is in
    > there, it crashes on the line where it tries to assign values to the
    > "lastError" session variable. But it passes If not isNothing() and
    > IsNumeric(). I don't get it. Can anyone see the problem in finding out
    > what is in this dataset? Thanks!
    >
    > Matt
    IfThenElse, Oct 16, 2007
    #2
    1. Advertising

  3. MattB

    MattB Guest

    Thanks for responding!
    I'm doing that in this snippet below in the catch block and it passes
    (it's not nothing). Then it errors where I'm assigning relevant info to
    the session variable.
    Any other ideas? I really do appreciate the response!

    Matt

    IfThenElse wrote:
    > try testing if r("qty_rem") is nothing first
    >
    >
    > "MattB" <> wrote in message
    > news:...
    >> I'm relatively familiar with asp.net and vb.net and have been working in
    >> the environment for several years. I've dealt with null reference errors
    >> before and generally know how to code around them
    >> But now I have some new code I deployed and I'm getting a null reference
    >> error when looping through a datatable (for each r in dt.rows...).
    >> I'm expecting to find an integer in this field, or possibly nothing, or
    >> possibly a dash (-).
    >> So I added some extra stuff to figure out what's in there (I don;t have
    >> access to the same dataset where this is running, or I'd just use the
    >> debugger). I made this block of code to help me figure it out:
    >>
    >> If IsNumeric(Trim(r("qty_rem"))) Then 'Trim(r("qty_rem")) <> "-" And
    >> Trim(r("qty_rem")) <> "" Then
    >> Dim iRem As Int16
    >> Try
    >> iRem = Convert.ToInt16(Trim(r("qty_rem")))
    >> Catch ex As Exception
    >> If Not IsNothing(r("qty_rem")) Then
    >> Current.Session("lastError") = "r(qty_rem) = '" & r("qty_rem") & "'" &
    >> vbCrLf & ex.Message & vbCrLf & ex.InnerException.Message
    >> Current.Response.Redirect("error.aspx")
    >> Else
    >> Current.Session("lastError") = "r(qty_rem) is nothing" & vbCrLf &
    >> ex.Message & vbCrLf & ex.InnerException.Message
    >> Current.Response.Redirect("error.aspx")
    >> End If
    >> End Try
    >>
    >> And now instead of redirecting to my error page and showing me what is in
    >> there, it crashes on the line where it tries to assign values to the
    >> "lastError" session variable. But it passes If not isNothing() and
    >> IsNumeric(). I don't get it. Can anyone see the problem in finding out
    >> what is in this dataset? Thanks!
    >>
    >> Matt

    >
    >
    MattB, Oct 17, 2007
    #3
  4. MattB

    IfThenElse Guest

    May be your ex.InnerException.Message is nothing. check it out.

    I use,
    If Not ( r("qty_rem") is nothing ) then
    may be the same as NOT IsNothing(etc..)





    "MattB" <> wrote in message
    news:...
    > Thanks for responding!
    > I'm doing that in this snippet below in the catch block and it passes
    > (it's not nothing). Then it errors where I'm assigning relevant info to
    > the session variable.
    > Any other ideas? I really do appreciate the response!
    >
    > Matt
    >
    > IfThenElse wrote:
    >> try testing if r("qty_rem") is nothing first
    >>
    >>
    >> "MattB" <> wrote in message
    >> news:...
    >>> I'm relatively familiar with asp.net and vb.net and have been working in
    >>> the environment for several years. I've dealt with null reference errors
    >>> before and generally know how to code around them
    >>> But now I have some new code I deployed and I'm getting a null reference
    >>> error when looping through a datatable (for each r in dt.rows...).
    >>> I'm expecting to find an integer in this field, or possibly nothing, or
    >>> possibly a dash (-).
    >>> So I added some extra stuff to figure out what's in there (I don;t have
    >>> access to the same dataset where this is running, or I'd just use the
    >>> debugger). I made this block of code to help me figure it out:
    >>>
    >>> If IsNumeric(Trim(r("qty_rem"))) Then 'Trim(r("qty_rem")) <> "-" And
    >>> Trim(r("qty_rem")) <> "" Then
    >>> Dim iRem As Int16
    >>> Try
    >>> iRem = Convert.ToInt16(Trim(r("qty_rem")))
    >>> Catch ex As Exception
    >>> If Not IsNothing(r("qty_rem")) Then
    >>> Current.Session("lastError") = "r(qty_rem) = '" & r("qty_rem") & "'" &
    >>> vbCrLf & ex.Message & vbCrLf & ex.InnerException.Message
    >>> Current.Response.Redirect("error.aspx")
    >>> Else
    >>> Current.Session("lastError") = "r(qty_rem) is nothing" & vbCrLf &
    >>> ex.Message & vbCrLf & ex.InnerException.Message
    >>> Current.Response.Redirect("error.aspx")
    >>> End If
    >>> End Try
    >>>
    >>> And now instead of redirecting to my error page and showing me what is
    >>> in there, it crashes on the line where it tries to assign values to the
    >>> "lastError" session variable. But it passes If not isNothing() and
    >>> IsNumeric(). I don't get it. Can anyone see the problem in finding out
    >>> what is in this dataset? Thanks!
    >>>
    >>> Matt

    >>
    IfThenElse, Oct 17, 2007
    #4
  5. MattB

    bruce barker Guest

    if it null in the database, r("qty_rem") returns DBNull, not null (nothing).

    -- bruce (sqlwork.com)

    MattB wrote:
    > I'm relatively familiar with asp.net and vb.net and have been working in
    > the environment for several years. I've dealt with null reference errors
    > before and generally know how to code around them
    > But now I have some new code I deployed and I'm getting a null reference
    > error when looping through a datatable (for each r in dt.rows...).
    > I'm expecting to find an integer in this field, or possibly nothing, or
    > possibly a dash (-).
    > So I added some extra stuff to figure out what's in there (I don;t have
    > access to the same dataset where this is running, or I'd just use the
    > debugger). I made this block of code to help me figure it out:
    >
    > If IsNumeric(Trim(r("qty_rem"))) Then 'Trim(r("qty_rem")) <> "-" And
    > Trim(r("qty_rem")) <> "" Then
    > Dim iRem As Int16
    > Try
    > iRem = Convert.ToInt16(Trim(r("qty_rem")))
    > Catch ex As Exception
    > If Not IsNothing(r("qty_rem")) Then
    > Current.Session("lastError") = "r(qty_rem) = '" &
    > r("qty_rem") & "'" & vbCrLf & ex.Message & vbCrLf &
    > ex.InnerException.Message
    > Current.Response.Redirect("error.aspx")
    > Else
    > Current.Session("lastError") = "r(qty_rem) is nothing" &
    > vbCrLf & ex.Message & vbCrLf & ex.InnerException.Message
    > Current.Response.Redirect("error.aspx")
    > End If
    > End Try
    >
    > And now instead of redirecting to my error page and showing me what is
    > in there, it crashes on the line where it tries to assign values to the
    > "lastError" session variable. But it passes If not isNothing() and
    > IsNumeric(). I don't get it. Can anyone see the problem in finding out
    > what is in this dataset? Thanks!
    >
    > Matt
    bruce barker, Oct 17, 2007
    #5
  6. MattB

    IfThenElse Guest

    good point

    "bruce barker" <> wrote in message
    news:eKp$h$...
    > if it null in the database, r("qty_rem") returns DBNull, not null
    > (nothing).
    >
    > -- bruce (sqlwork.com)
    >
    > MattB wrote:
    >> I'm relatively familiar with asp.net and vb.net and have been working in
    >> the environment for several years. I've dealt with null reference errors
    >> before and generally know how to code around them
    >> But now I have some new code I deployed and I'm getting a null reference
    >> error when looping through a datatable (for each r in dt.rows...).
    >> I'm expecting to find an integer in this field, or possibly nothing, or
    >> possibly a dash (-).
    >> So I added some extra stuff to figure out what's in there (I don;t have
    >> access to the same dataset where this is running, or I'd just use the
    >> debugger). I made this block of code to help me figure it out:
    >>
    >> If IsNumeric(Trim(r("qty_rem"))) Then 'Trim(r("qty_rem")) <> "-" And
    >> Trim(r("qty_rem")) <> "" Then
    >> Dim iRem As Int16
    >> Try
    >> iRem = Convert.ToInt16(Trim(r("qty_rem")))
    >> Catch ex As Exception
    >> If Not IsNothing(r("qty_rem")) Then
    >> Current.Session("lastError") = "r(qty_rem) = '" &
    >> r("qty_rem") & "'" & vbCrLf & ex.Message & vbCrLf &
    >> ex.InnerException.Message
    >> Current.Response.Redirect("error.aspx")
    >> Else
    >> Current.Session("lastError") = "r(qty_rem) is nothing" &
    >> vbCrLf & ex.Message & vbCrLf & ex.InnerException.Message
    >> Current.Response.Redirect("error.aspx")
    >> End If
    >> End Try
    >>
    >> And now instead of redirecting to my error page and showing me what is in
    >> there, it crashes on the line where it tries to assign values to the
    >> "lastError" session variable. But it passes If not isNothing() and
    >> IsNumeric(). I don't get it. Can anyone see the problem in finding out
    >> what is in this dataset? Thanks!
    >>
    >> Matt
    IfThenElse, Oct 17, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,526
    Mike Schilling
    Mar 29, 2006
  2. Replies:
    1
    Views:
    470
    Mark Rae [MVP]
    Sep 20, 2007
  3. dorayme
    Replies:
    1
    Views:
    605
    richard
    Jan 21, 2011
  4. richard
    Replies:
    0
    Views:
    572
    richard
    Jan 21, 2011
  5. will
    Replies:
    6
    Views:
    399
    Phrogz
    Dec 27, 2006
Loading...

Share This Page