Check if Column exists

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Is there a way to check if a column exists in a datarow before I try to
access it's data?
I am reading an Xml document into a DataSet which may or may not have the
data I want to display to the user in a textbox. The Xml document does not
have a schema so the DataSet creates it's structure from the information
present.
I don't really want to put try/catch blocks around each column, is there a
better way to do it?

Thanks,
Steve
 
You certainly could check it via DataRow's Table property like
dr.Table.Columns.Contains("myColumn") but if I understand your scenario
right, it might not be what you are looking for. Can you clarify a bit in
that case?
 
I have created a solution to my problem by creating a function that checks
each column in a DataTable. I think this is the only way to do it:

private bool ColumnExists(DataTable Table, string ColumnName)
{
bool bRet = false;
foreach(DataColumn col in Table.Columns)
{
if (col.ColumnName == ColumnName)
{
bRet = true;
break;
}
}

return bRet;
}
 
Back
Top