Checking for columns in a DataRow

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

Guest

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,
 
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();
 
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...
}
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top