Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
204 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
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 …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

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