Checking for columns in a DataRow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • randy1200

    Checking for columns in a DataRow

    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,
    --
    Randy
  • dllhell

    #2
    Re: Checking for columns in a DataRow


    "randy1200" <randy1200@news groups.nospamwr ote in message
    news:457FADA7-B988-4358-BE18-263B0FCE11FC@mi crosoft.com...
    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();



    Comment

    • Bala

      #3
      Re: Checking for columns in a DataRow

      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...
      }

      randy1200 wrote:
      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,
      --
      Randy

      Comment

      • Rader

        #4
        Re: Checking for columns in a DataRow

        use method "Contains(strin g)" of DataColumnColle ction to check if the
        column exists:
        if(dt.Columns.C ontains(colName )
        string theName = dr["colName"].ToString();

        another choice is selecting with col index

        randy1200 wrote:
        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,
        --
        Randy

        Comment

        Working...