Validate Field Existence


Hello,

Does anyone know how to determine if a field exists in an database without using try..catch like describe below. Thank for the advise.

try
id = dt.Rows(0)("id").tostring
catch
end try
LVL 10
ericwong27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VerminedlCommented:
What about:

        If dt.Rows(0).IsNull("ID") = False Then
            id = dt.Rows(0)("ID").ToString
        Else
            'ID was NULL
        End If
appariCommented:

add the following function

private function checkColumnExists(dt as datatable, sColName as string)
       For Each dc As DataColumn In dt.Columns      
            If i.ColumnName.Equals(sColName) Then
                    return true
                End If
      Next
      return false
end function

            
call it like this

if checkColumnExists(dt,"id") then
       messagebox.show "Column Exists"
else
       messagebox.show "Column Not Exists"
endif
appariCommented:
small change in the function

private function checkColumnExists(dt as datatable, sColName as string)
      For Each dc As DataColumn In dt.Columns    
          If dc.ColumnName.Equals(sColName) Then
                    return true
          End If
     Next
     return false
end function

amyhxuCommented:
You can use DataColumnCollection.Contains Method to confirm the existence of a column.

    Dim cols As DataColumnCollection = dt.Columns
    If cols.Contains("id") Then
        Messagebox.show("Column Exists")
    Else
        Messagebox.show("Column Not Exists")
    End If

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ericwong27Author Commented:
Thank :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.