Search code examples
c#csvhelper

Write to a File using CsvHelper in C#


I tried to write to CSV file using CsvHelper in C#.
This is the link to the library http://joshclose.github.io/CsvHelper/

I used the code in web site.

Here is my code:

var csv = new CsvWriter(writer);
csv.Configuration.Encoding = Encoding.UTF8;
foreach (var value in valuess)
{
    csv.WriteRecord(value);
}

It writes only a part of data to csv file.
Last rows were missing.
Could you please help with this.


Solution

  • when, I added this code after the loop code is working well

    var csv = new CsvWriter(writer);
    csv.Configuration.Encoding = Encoding.UTF8;
    foreach (var value in valuess)
    {
         csv.WriteRecord(value);
    }
    writer.Close();
    

    The problem occurred because I did not close the Connection