iterating thru datatables collection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cm9kY2hhcg==?=

    iterating thru datatables collection

    hey all,
    i have a loop like the following:

    foreach(DataTab le dt in DataTablesColle ction1)
    {
    if(dt[0][0].ToString().Con tains("somethin g")
    {
    //do something
    }
    }

    the problem is when i get an empty datatable and it doesn't find a row at
    the position in the if statement. how do you avoid something like that?

    thanks,
    rodchar
  • Alberto Poblacion

    #2
    Re: iterating thru datatables collection

    "rodchar" <rodchar@discus sions.microsoft .comwrote in message
    news:68CFB37F-0173-48D3-8181-9A2070E99D04@mi crosoft.com...
    hey all,
    i have a loop like the following:
    >
    foreach(DataTab le dt in DataTablesColle ction1)
    {
    if(dt[0][0].ToString().Con tains("somethin g")
    {
    //do something
    }
    }
    >
    the problem is when i get an empty datatable and it doesn't find a row at
    the position in the if statement. how do you avoid something like that?
    Add a statement to check the number of rows:

    if (dt.Rows.Count> 0) ...


    Comment

    • Pete Kane

      #3
      Re: iterating thru datatables collection

      rodchar wrote:
      hey all,
      i have a loop like the following:
      >
      foreach(DataTab le dt in DataTablesColle ction1)
      {
      if(dt[0][0].ToString().Con tains("somethin g")
      {
      //do something
      }
      }
      >
      the problem is when i get an empty datatable and it doesn't find a row at
      the position in the if statement. how do you avoid something like that?
      >
      thanks,
      rodchar
      put another if before

      if(dt[0][0].ToString().Con tains("somethin g")

      like

      if(dt[0].Rows.Count 0)

      Comment

      • Mark Peters

        #4
        Re: iterating thru datatables collection

        >the problem is when i get an empty datatable and it doesn't find a row at
        >the position in the if statement. how do you avoid something like that?
        if (dt.Rows.Count == 0) {// Do something else; continue;}

        try/catch maybe?

        How expected is an empty table?
        What are you trying to do?

        Comment

        • =?Utf-8?B?cm9kY2hhcg==?=

          #5
          RE: iterating thru datatables collection

          thanks everyone for the help,
          rod.

          "rodchar" wrote:
          hey all,
          i have a loop like the following:
          >
          foreach(DataTab le dt in DataTablesColle ction1)
          {
          if(dt[0][0].ToString().Con tains("somethin g")
          {
          //do something
          }
          }
          >
          the problem is when i get an empty datatable and it doesn't find a row at
          the position in the if statement. how do you avoid something like that?
          >
          thanks,
          rodchar

          Comment

          Working...