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
196 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 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now