Solved

Datareader - checking for system,indexOutOfRangeException

Posted on 2014-02-12
1
195 Views
Last Modified: 2014-02-26
I have a data reader that has this code

while (dr.Read())
                {
 if (Convert.ToInt16(dr[0]) > 0) {     
                    recordsReturned = 1;
                    }

Open in new window


This works fine if results are returned, but if the data reader has no results, it breaks
I tried using .HasRows, but it always has rows for some reason

Anyway, what code do i need to check for this out of range exception?
0
Comment
Question by:websss
1 Comment
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 39853191
Check for dbNull:

while (dr.Read())
 {
 if (r[0] != DBNull.value && Convert.ToInt16(dr[0]) > 0) 
   {     
      recordsReturned = 1;
   }
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question