Solved

Datareader - checking for system,indexOutOfRangeException

Posted on 2014-02-12
1
196 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 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