Link to home
Start Free TrialLog in
Avatar of ManageMobility
ManageMobility

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Priest04
Priest04
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ManageMobility
ManageMobility

ASKER

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.
There is no other way, unless you want to catch an exception, which is no recommended.
That's what I'm afraid of. I may have to just take on the extra loop.
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. :)
Good point :)