c# - Creating a csv file, not adding to the next column -


first time trying build csv file in c#, here code:

string filepath = @"c:\users\me\downloads\csvfiles\test-" + datetime.now.tostring("dd-mm-yyyy-hh-mm-ss") + ".csv"; string delimit = ","; list<string> cells = new list<string>();  console.writeline("gathering data...");  sqlconnection con = new sqlconnection(properties.settings.default.connectionstring); con.open(); sqlcommand cmd = new sqlcommand(); cmd.connection = con; cmd.commandtype = system.data.commandtype.text; cmd.commandtext = "select id, name contacts";  sqldatareader reader = cmd.executereader();  console.writeline("building csv file");  while (reader.read()) {     addcell((idatarecord)reader, cells); }  stringbuilder sb = new stringbuilder();  (int = 0; < cells.count; i++) {     sb.appendline(string.join(delimit, cells[i])); }  file.writealltext(filepath, sb.tostring());  /*method add each record list*/ public static void addcell(idatarecord record, list<string> cells) {     cells.add(record[0].tostring());     cells.add(record[1].tostring()); } 

trouble is, appends new row, doesn't move record[1] next column , start new row...

your loop incorrect trying do.

replace :

    (int = 0; < cells.count; i++)     {          sb.appendline(string.join(delimit, cells[i]));     } 

with

    int = 0;     while (i < cells.count)     {        sb.appendline(string.join(delimit, cells[i],cells[i+1]));        = + 2;     } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

angularjs - ng-repeat duplicating items after page reload -