Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Accessing fields in a datatable

Accessing fields in a datatable

Scheduled Pinned Locked Moved C#
help
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M maliary

    Hi, I have this code for looping through the fields in a data table and replace some characters in the fields. I am trying to access the fields in the datatable. Its giving this error : System.InvalidOperationException: Collection was modified; enumeration operation might not execute. Could you help me through this, I'd really appreciate it. foreach (DataRow dr in dtExport.Rows) { for (int i = 0; i < dtExport.Columns.Count; i++) { if (!Convert.IsDBNull(dr[i])) { data = dr[i].ToString(); data = data.Replace("\"", "\""); RowZ.Add(data.ToString()); } } dtExport.Rows.Add(RowZ.ToArray()); }

    Let's do this !

    modified on Thursday, August 14, 2008 10:27 AM

    C Offline
    C Offline
    Colin Angus Mackay
    wrote on last edited by
    #2

    You cannot add or remove elements in a collection (in this case dtExport) that you are iterating over.

    Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

    M 1 Reply Last reply
    0
    • M maliary

      Hi, I have this code for looping through the fields in a data table and replace some characters in the fields. I am trying to access the fields in the datatable. Its giving this error : System.InvalidOperationException: Collection was modified; enumeration operation might not execute. Could you help me through this, I'd really appreciate it. foreach (DataRow dr in dtExport.Rows) { for (int i = 0; i < dtExport.Columns.Count; i++) { if (!Convert.IsDBNull(dr[i])) { data = dr[i].ToString(); data = data.Replace("\"", "\""); RowZ.Add(data.ToString()); } } dtExport.Rows.Add(RowZ.ToArray()); }

      Let's do this !

      modified on Thursday, August 14, 2008 10:27 AM

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #3

      Try using a for loop instead of for each. That should solve your problem

      Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      M 1 Reply Last reply
      0
      • C Colin Angus Mackay

        You cannot add or remove elements in a collection (in this case dtExport) that you are iterating over.

        Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

        M Offline
        M Offline
        maliary
        wrote on last edited by
        #4

        This, which comes from a diffrent part of the code works

        foreach (DataRow dr in TheDataTable.Rows)
        {
        for (int i = 0; i < iColCount; i++)
        {
        if (!Convert.IsDBNull(dr[i]))
        {
        string data = dr[i].ToString();
        data = data.Replace("\"","\"");
        sw.Write(data);
        }
        if (i < iColCount - 1)
        {
        sw.Write(separator);
        }
        }
        sw.Write(sw.NewLine);
        }

        the diffrence with the other one is that, I want to put the elements back into a datatable, for other processing. I how do I make the change of the field values, then move the values into a data table again ?

        Let's do this !

        1 Reply Last reply
        0
        • M Manas Bhardwaj

          Try using a for loop instead of for each. That should solve your problem

          Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

          M Offline
          M Offline
          maliary
          wrote on last edited by
          #5

          I changed it to this:

          for (int j = 0; j < dtExport.Rows.Count; j++)
          {

                          for (int i = 0; i < dtExport.Columns.Count; i++)
                          {
                              if (!Convert.IsDBNull(data = dtExport.Rows\[j\]\[i\].ToString()))
                              {
                                  data = dtExport.Rows\[j\]\[i\].ToString();
          
                                  data = data.Replace("\\"", "\\"");
                                  RowZ.Add(data.ToString());
                              }
          
                          }
          dtExport.Rows.Add(RowZ.ToArray());
          
                         
                      }
          

          but now I get this error System.ArgumentException:input array is longer than the number of columns in the table

          Let's do this !

          C 1 Reply Last reply
          0
          • M maliary

            I changed it to this:

            for (int j = 0; j < dtExport.Rows.Count; j++)
            {

                            for (int i = 0; i < dtExport.Columns.Count; i++)
                            {
                                if (!Convert.IsDBNull(data = dtExport.Rows\[j\]\[i\].ToString()))
                                {
                                    data = dtExport.Rows\[j\]\[i\].ToString();
            
                                    data = data.Replace("\\"", "\\"");
                                    RowZ.Add(data.ToString());
                                }
            
                            }
            dtExport.Rows.Add(RowZ.ToArray());
            
                           
                        }
            

            but now I get this error System.ArgumentException:input array is longer than the number of columns in the table

            Let's do this !

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #6

            I don't see any definition for RowZ, does it have more columns than the rows in the dtExport table?

            Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

            M 1 Reply Last reply
            0
            • C Colin Angus Mackay

              I don't see any definition for RowZ, does it have more columns than the rows in the dtExport table?

              Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

              M Offline
              M Offline
              maliary
              wrote on last edited by
              #7

              This is the definition, Arraylist Rowz = new Arraylist () Its an array list

              Let's do this !

              C 1 Reply Last reply
              0
              • M maliary

                This is the definition, Arraylist Rowz = new Arraylist () Its an array list

                Let's do this !

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #8

                Okay, I see. On the second loop you continue to add to the array RowZ, this means that it contains the column values for two rows by the time it gets to the dtExport.Rows.Add method. Since the array has more items in it than columns, the Add method fails as it doesn't know what to do with all that extra data. At the start of the loop over each row, you need to clear out the array.

                RowZ.Clear();

                Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                1 Reply Last reply
                0
                • M maliary

                  Hi, I have this code for looping through the fields in a data table and replace some characters in the fields. I am trying to access the fields in the datatable. Its giving this error : System.InvalidOperationException: Collection was modified; enumeration operation might not execute. Could you help me through this, I'd really appreciate it. foreach (DataRow dr in dtExport.Rows) { for (int i = 0; i < dtExport.Columns.Count; i++) { if (!Convert.IsDBNull(dr[i])) { data = dr[i].ToString(); data = data.Replace("\"", "\""); RowZ.Add(data.ToString()); } } dtExport.Rows.Add(RowZ.ToArray()); }

                  Let's do this !

                  modified on Thursday, August 14, 2008 10:27 AM

                  A Offline
                  A Offline
                  acqy
                  wrote on last edited by
                  #9

                  Do not put your updating statement within the foreach scope. Try use "for(int i=0;i

                  M 1 Reply Last reply
                  0
                  • A acqy

                    Do not put your updating statement within the foreach scope. Try use "for(int i=0;i

                    M Offline
                    M Offline
                    maliary
                    wrote on last edited by
                    #10

                    Hi, I have done that, and also placing the Row.Clear before the rows, but I still get the error: input array is longer than the number of columns in this table error ? Where should I have it ?

                                for (int j = 0; j < dpExport.Rows.Count; j++)
                                {
                                     Row.Clear();
                    
                                    for (int i = 0; i < dpExport.Columns.Count; i++)
                                    {
                                        
                                        if (!Convert.IsDBNull(dpExport.Rows\[j\]\[i\].ToString()))
                                        {
                                            
                                            data = dpExport.Rows\[j\]\[i\].ToString();
                                            data = data.Replace("\\"", "\\"");
                                            Row.Add(data.ToString());
                                        }
                    
                                    }
                                    dtExport.Rows.Add(Row.ToArray());
                                }
                    

                    Let's do this !

                    A 1 Reply Last reply
                    0
                    • M maliary

                      Hi, I have done that, and also placing the Row.Clear before the rows, but I still get the error: input array is longer than the number of columns in this table error ? Where should I have it ?

                                  for (int j = 0; j < dpExport.Rows.Count; j++)
                                  {
                                       Row.Clear();
                      
                                      for (int i = 0; i < dpExport.Columns.Count; i++)
                                      {
                                          
                                          if (!Convert.IsDBNull(dpExport.Rows\[j\]\[i\].ToString()))
                                          {
                                              
                                              data = dpExport.Rows\[j\]\[i\].ToString();
                                              data = data.Replace("\\"", "\\"");
                                              Row.Add(data.ToString());
                                          }
                      
                                      }
                                      dtExport.Rows.Add(Row.ToArray());
                                  }
                      

                      Let's do this !

                      A Offline
                      A Offline
                      acqy
                      wrote on last edited by
                      #11

                      OK, now it seems that we cannot update the dpExport within the loop. Try caching the rows to a local collection and after the loop update all the rows in the cache. I don't know if this helps.

                      Sunny Chen =============================================== System Analyst, System Architect Consultant of China System Analyst Institution
                      http://www.sunnychen.org ===============================================

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups