简体   繁体   中英

C# DataTable error

I have a DataTable printed on a ListView, it was working fine, but at some point, it started trhowing theese errors. The workaround for this project is:

User fills the WinForms, then inserts on DataBase, when the user finish, the MainForm is shown, calling the actualizarFormulario() method, so the ListView is filled with the new data.

EDIT

The line 156 in this error is item.SubItems.Add(row[1].ToString()); but it also gives me 153, 155... everything inside that foreach.

21-05 17:00 > Exception: Tipo: System.InvalidOperationException Mensaje: Collection was modified; enumeration operation might not execute. Origen: System.Data Stacktrace: at System.Data.RBTree`1.RBTreeEnumerator.MoveNext() at Operaciones_Diversas.Principal.actualizarFormulario() in C:\\Documents and Settings\\usuario\\mis documentos\\visual studio 2010\\Projects\\Operaciones Diversas\\Operaciones Diversas\\Principal.cs:line 156


The code for fill the data is this

private void actualizarFormulario()
{
    try
    {
        listaLotes.Items.Clear();
        foreach (DataRow row in Consultas.listadoLotes().Rows)
        {
            ListViewItem item = new ListViewItem(row[0].ToString());
            item.SubItems.Add(row[1].ToString());
            item.SubItems.Add(Convert.ToDecimal(row[2].ToString().Substring(0, row[2].ToString().Length - 2) + "," + row[2].ToString().Substring(row[2].ToString().Length - 2, 2)).ToString("N2", Cultures.Spain));
            item.SubItems.Add(row[3].ToString());
            item.SubItems.Add(row[4].ToString());
            listaLotes.Items.Add(item);
        }
    }
    catch (Exception ex) { Logger.log(ex); }
}

    public static DataTable listadoLotes()
    {
        try
        {
            SelectBD sel = new SelectBD(Program.ConexBD,
            "SELECT referencia, tipo, total_lote, COUNT(Documentos.id) as Documentos, cuenta FROM Lotes"
            + " LEFT JOIN Documentos"
            + " ON Lotes.referencia = Documentos.ref_lote"
            + " WHERE Lotes.fecha_creacion='" + valoresGenerales.dateHoy + "'"
            + " GROUP BY Lotes.referencia, Lotes.tipo, Lotes.total_lote, Lotes.cuenta"
            + " ORDER BY Lotes.tipo"
            );
            return sel.DataTable;
        }
        catch (Exception ex)
        {
            Logger.log(ex);
            return new DataTable();
        }
    }

EDIT 2

Using a for loop, is increasing my program speed, and it can't be this way, because the user needs to interact fast with everything...

for (int i = 0; i < Consultas.listadoLotes().Rows.Count; i++)
{
    ListViewItem item = new ListViewItem(Consultas.listadoLotes().Rows[i]["referencia"].ToString());
    item.SubItems.Add(Consultas.listadoLotes().Rows[i]["tipo"].ToString());
    item.SubItems.Add(Convert.ToDecimal(Consultas.listadoLotes().Rows[i]["total_lote"].ToString()
        .Substring(0, Consultas.listadoLotes().Rows[i]["total_lote"].ToString().Length - 2)
        + ","
        + Consultas.listadoLotes().Rows[i]["total_lote"].ToString()
        .Substring(Consultas.listadoLotes().Rows[i]["total_lote"].ToString().Length - 2, 2)).ToString("N2", Cultures.Spain));
    item.SubItems.Add(Consultas.listadoLotes().Rows[i]["Documentos"].ToString());
    item.SubItems.Add(Consultas.listadoLotes().Rows[i]["cuenta"].ToString());
    listaLotes.Items.Add(item);
}

EDIT 3 Working Code

        listaLotes.Items.Clear();
        DataTable tabla = Consultas.listadoLotes();
        for (int i = 0; i < tabla.Rows.Count; i++)
        {
            ListViewItem item = new ListViewItem();
            item.SubItems.Add(tabla.Rows[i]["referencia"].ToString());
            item.SubItems.Add(tabla.Rows[i]["tipo"].ToString());
            item.SubItems.Add(Convert.ToDecimal(tabla.Rows[i]["total_lote"].ToString()
                .Substring(0, tabla.Rows[i]["total_lote"].ToString().Length - 2)
                + ","
                + tabla.Rows[i]["total_lote"].ToString()
                .Substring(tabla.Rows[i]["total_lote"].ToString().Length - 2, 2)).ToString("N2", Cultures.Spain));
            item.SubItems.Add(tabla.Rows[i]["Documentos"].ToString());
            item.SubItems.Add(tabla.Rows[i]["cuenta"].ToString());
            listaLotes.Items.Add(item);
        }

You're modifying the collection as you're iterating through the enumerable. Instead of a foreach loop, use a for loop instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM