Error traversing the rows of a table in C #

0

I already have the basic or syntactic structure that should make my loop work, but for some reason I get this error:

  

Error 20 The foreach statement can not work on variables of   type 'System.Data.DataTable' because 'System.Data.DataTable' does not   contains no public definition for   'GetEnumerator' C: \ Users \ NQ054 \ Desktop \ SGSIA \ App_Code \ Configuration \ Empresa.cs 141 8 C: ... \ SGSIA \

The syntax of my foreach is the following

   foreach (DataRow datos in plantas)
   {
       crearHTML(datos.ToString);
   }

The creation of my table is as follows:

    storedProcedure sp = new storedProcedure("DBSGSIAConnectionString");
   DataTable plantas;
   plantas = new DataTable();
   string planta;
    string query = "select * from v_DatosEmpresa;";
    plantas = sp.getValues(query);
   planta= Convert.ToString(plantas);
    
asked by David 24.01.2017 в 20:44
source

1 answer

3

Change this line:

foreach (DataRow datos in plantas)

for this one:

foreach (DataRow datos in plantas.rows)

This way it can also work:

foreach (var datos in plantas.rows)

Explanation:

You can not iterate the DataTable object, but if you can iterate through the collection of rows it contains, then the error is derived.

Greetings.

    
answered by 24.01.2017 / 20:58
source