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
195 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
Comment Utility

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
Comment Utility
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
Comment Utility
There is no other way, unless you want to catch an exception, which is no recommended.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

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

Expert Comment

by:Priest04
Comment Utility
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
Comment Utility
Good point :)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Read about why website design really matters in today's demanding market.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

15 Experts available now in Live!

Get 1:1 Help Now