[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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
?
206 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 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