?
Solved

How do I test to see if an index is within a range (or exists) without throwing an error?

Posted on 2008-10-07
6
Medium Priority
?
203 Views
Last Modified: 2013-11-19
I am pulling fields that may or may not be contained in the SqlDataReader called ReaderOrderInfo.
How can I test the datareader if it contains this index before attempting to pull it (which throws an error)?
The site is coded in VB .NET
Thanks!
If Not ReaderOrderInfo(oiNode.FieldName) Is Nothing Then
  oiNode.Value = ReaderOrderInfo(oiNode.FieldName)
End If

Open in new window

0
Comment
Question by:ManageMobility
[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
  • 3
  • 3
6 Comments
 
LVL 18

Accepted Solution

by:
Priest04 earned 2000 total points
ID: 22662649

Dim fieldExists As Boolean = False
 
For i As Integer = 0 To ReaderOrderInfo.FieldCount - 1
    If ReaderOrderInfo.GetName(i) = oiNode.FieldName Then
        fieldExists = True
        Exit For
    End If
Next
 
If fieldExists Then
    ' code
End If

Open in new window

0
 
LVL 2

Author Comment

by:ManageMobility
ID: 22662682
I had considered something similar to this method, but I've already got this function nested within two other For loops, so I was hoping to have some sort of "check this index" way to test of the index existed within the data reader, so not to add another layer of loops to the page.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22662786
There is no other way, unless you want to catch an exception, which is no recommended.
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 2

Author Comment

by:ManageMobility
ID: 22662793
That's what I'm afraid of. I may have to just take on the extra loop.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22662816
and btw, what exactly do you think data provider would do if there is a method like

if (dr.Columnexist(columnname))
{
}

it ould neeed to loop through all column, right? The code would just look better, abstraction in action. :)
0
 
LVL 2

Author Comment

by:ManageMobility
ID: 22662963
Good point :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

762 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