I have a DataSet/DataTable read in from an XML file. I can loop through the
DataRows in the DataTable, and do actions like this:
string theName = dr["Name"].ToString();
My fear is that somebody is going to come along and slightly change the xml
file, perhaps changing "Name" to "Names". The rather brittle line above will
throw an exception.
I'd really like to do something like the following:
if(dr["Name"].Exists)
string theName = dr["Name"].ToString();
As far as I can tell, the .Exists doesn't exist in this context in C#.
Any suggestions on how to approach this, other than just catching the
exception?
Thanks,

Signature
Randy
dllhell - 14 Aug 2006 14:21 GMT
> I'd really like to do something like the following:
>
> if(dr["Name"].Exists)
> string theName = dr["Name"].ToString();
try this:
string theName = dr[index_of_column].ToString();
Bala - 14 Aug 2006 14:24 GMT
Hi,
Can't you just do:
Object oDataRowValue = dr["Name"];
if (oDataRowValue != null)
{
string theName = dr["Name"].ToString();
}
else
{
// Whatever you planned to do on error...
}
> I have a DataSet/DataTable read in from an XML file. I can loop through the
> DataRows in the DataTable, and do actions like this:
[quoted text clipped - 16 lines]
>
> Thanks,
Rader - 14 Aug 2006 14:39 GMT
use method "Contains(string)" of DataColumnCollection to check if the
column exists:
if(dt.Columns.Contains(colName)
string theName = dr["colName"].ToString();
another choice is selecting with col index
> I have a DataSet/DataTable read in from an XML file. I can loop through the
> DataRows in the DataTable, and do actions like this:
[quoted text clipped - 16 lines]
>
> Thanks,