Weird Null reference error problem

M

MattB

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
 
M

MattB

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
 
I

IfThenElse

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..)
 
B

bruce barker

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

-- bruce (sqlwork.com)
 

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