kingkuber
kingkuber

Reputation:

Check if a column exists in vb.net, ADO.net

I have a windows service which fetches data from various datasources and build a XML file and send it to a webservice. For example first I will get customer details from Oracle Database and build the XML file and then the SQL Server database and build the customer details XML. I am planning to use the same function below to build the customer object irrespective of what the datasource is. But dr("age") column is not available in SQLserver datbase How can I check if a column exists or not. I want to do something like

if dr("age") exists then
.age=dr("age")
else
.age=0
end if

I have a function as shown below which populates the customer Object. As I am

Public Shared Function Retrieve() As List(Of Customer)
Dim dt As DataTable = Dac.ExecuteDataTable( _ 
  "CustomerRetrieveAll", nothing)
Dim customerList As New List(Of Customer)
For Each dr As DataRow In dt.Rows   
 customerList.Add(New Customer With _
                 {.CustomerId = CType(dr("CustomerID"), Integer), _                  .LastName = dr("LastName").ToString, _   
              .age = dr("age").ToString, _                  
.FirstName = dr("FirstName").ToString})  
Next
Return customerList
End Function

Upvotes: 0

Views: 6459

Answers (3)

you can use the method contains of the table that ows the datarow. like:

If row.Table.Columns.Contains("your table name") Then ....

JDVA

Upvotes: 0

David Andres
David Andres

Reputation: 31781

There is a DataTable.Columns collection that you can inspect to determine if a column exists.

Upvotes: 0

Chris
Chris

Reputation: 3712

DataColumnCollection.Contains

Upvotes: 4

Related Questions