database - C# DataTable error -
i have datatable printed on listview, working fine, @ point, started trhowing theese errors. workaround project is:
user fills winforms, inserts on database, when user finish, mainform shown, calling actualizarformulario()
method, listview
filled new data.
edit
the line 156 in error item.subitems.add(row[1].tostring());
gives me 153, 155... inside foreach.
21-05 17:00 > exception: tipo: system.invalidoperationexception mensaje: collection modified; enumeration operation might not execute. origen: system.data stacktrace: @ system.data.rbtree`1.rbtreeenumerator.movenext() @ operaciones_diversas.principal.actualizarformulario() in c:\documents , settings\usuario\mis documentos\visual studio 2010\projects\operaciones diversas\operaciones diversas\principal.cs:line 156
the code fill data 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) documentos, cuenta lotes" + " left join documentos" + " on lotes.referencia = documentos.ref_lote" + " lotes.fecha_creacion='" + valoresgenerales.datehoy + "'" + " group lotes.referencia, lotes.tipo, lotes.total_lote, lotes.cuenta" + " order lotes.tipo" ); return sel.datatable; } catch (exception ex) { logger.log(ex); return new datatable(); } }
edit 2
using for
loop, increasing program speed, , can't way, because user needs interact fast everything...
for (int = 0; < 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(); (int = 0; < 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 collection you're iterating through enumerable. instead of foreach
loop, use for
loop instead.
Comments
Post a Comment