Solved

Datareader - checking for system,indexOutOfRangeException

Posted on 2014-02-12
1
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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