Don't Understand This error?

W

Wayne Wengert

I am using a datareader (dr1) to read through rows returned from a query. I
am getting the error: "No data exists for the row/column." at the "If
IsDbNull..." in the code below. The field "Photo1" does exist and in some
rows it is null, in others it may be an empty string and in others it may
have a file name. I don't understand exactly what the system is complaining
about here?

Wayne



=========== code ============
If IsDBNull(dr1.Item("Photo2")) Then

txtPhoto2.Text = ""

Else

txtPhoto2.Text = dr1.Item("Photo2")

End If
 
M

Manohar Kamath

Where's the complete code, are you doing a while dr1.Read() before you check
for null?


While dr1.Read()
If IsDBNull(dr1.Item("Photo2")) Then
End
 
M

Mark Fitzpatrick

If a column in a row doesn't exist, then IsDbNull will not be able to
evaluate the contents of the cell because there aren't any contents of the
cell. If the field doesn't exist you need to catch the
IndexOutOfRangeException exception that it will throw. Remember, a null
value in a database is a special value being passed from the db and not the
same as a null used in typical programming languages.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
W

Wayne Wengert

The "complete code" is over 600 lines with may subs and functions being
called. I am getting suspicious that I may have a call to a sub that closes
the reader - I'll put in some brakes to see if I can isolate it.

Wayne
 
S

Steve C. Orr [MVP, MCSD]

I'm using the same code in my software and it works like a champ.
This leads me to believe that maybe you don't actually have a "Photo2"
column in your result set as you think you do.
Try this experiment, use the numeric index of the data column instead of the
column name, such as this:
If IsDBNull(dr1.Item(0)) Then...
 
W

Wayne Wengert

I had a sub that closed the reader under certain conditions. That was
causing the error. Fixed the logic and now it is working.

I appreciate the suggestions.

Wayne
 
G

George Christopher

Can u please explain what a sub means and how the sub was responsible
for the error. I am in a similar situation and not able to understand
how the exception is thrown.

George
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top