c# - ERROR: Cannot find column 10 -
i trying import csv file database. file has 10 columns. testing purposes first imported firs 4 columns , worked fine when try import columns error saying cannot find column 10.
this code
`
datatable dt = new datatable(); dt.columns.addrange(new datacolumn[10] { new datacolumn("invoice", typeof(string)), new datacolumn("p.o.number", typeof(string)), new datacolumn("line", typeof(int)), new datacolumn("invoice_date",typeof(datetime)), new datacolumn("gross", typeof(string)), new datacolumn("disc", typeof(string)), new datacolumn("net", typeof(string)), new datacolumn("date_pd", typeof(string)), new datacolumn("check#_doc#", typeof(string)), new datacolumn("additional_info", typeof(string))}); string csvdata = file.readalltext(csvpath); foreach (string row in csvdata.split('\n')) { if (!string.isnullorempty(row)) { dt.rows.add(); int = 0; foreach (string cell in row.split(',')) { dt.rows[dt.rows.count - 1][i] = cell; i++; } } }`
your index of columns starts @ 0 , ends @ 9 not 10. trying add column index not exist.
int = 0; foreach (string cell in row.split(',')) { if(i == 10) throw new argumentoutofrangeexception(nameof(i)); dt.rows[dt.rows.count - 1][i] = cell; i++; }
the exception prevent bad data being written datasource if have ,
commas in source.
i suggest investing in time in converting source |
pipe delimited separated file ,
commas can commonly used in data itself.
Comments
Post a Comment