CsvHelper does not read data from .csv-File created by CsvHelper

El Hippo

Hi I am trying to use CsvHelper and did finally get it to write my data into a .csv-file but if try to read the data the result is always empty. It reads the header and then csv.Read() seems to skip all the data.

The Sperrnummer class definition:

public class Sperrnummer
{
    public string Nummer { get; set; }
    public DateTime Erstellt { get; set; }
    public string Benutzer { get; set; }

    public Sperrnummer(string nummer, DateTime erstellt, string benutzer)
    {
        Nummer = nummer;
        Erstellt = erstellt;
        Benutzer = benutzer;
    }

    public Sperrnummer(string nummer)
    {
        Nummer = nummer;
        Erstellt = DateTime.Now;
        Benutzer = HttpContext.Current.User.Identity.Name;
    }
    public Sperrnummer()
    {
        Nummer = "";
        Erstellt = DateTime.Now;
        Benutzer = HttpContext.Current.User.Identity.Name;
    }
}

Here's my sample file:

Nummer,Erstellt,Benutzer
03063651976,08.07.2018 13:54:49,MASCHINE\Plopp
03063651977,08.07.2018 13:54:49,MASCHINE\Plopp
03063651978,08.07.2018 13:54:49,MASCHINE\Plopp
03063651979,08.07.2018 13:54:49,MASCHINE\Plopp
03063651971,08.07.2018 13:54:49,MASCHINE\Plopp
03063651972,08.07.2018 13:54:49,MASCHINE\Plopp
03063651973,08.07.2018 13:54:49,MASCHINE\Plopp

And here's my "Reader":

public ActionResult Index()
{
    var filePath = Server.MapPath("~/liste.txt");
    List<Sperrnummer> sperrnummern = new List<Sperrnummer>();

    if (System.IO.File.Exists(filePath)){
        using (var reader = new StringReader(filePath))
        {
            var csv = new CsvReader(reader);

            csv.Read();
            csv.ReadHeader();
            while (csv.Read())
            {
                var nummer = csv.GetRecord<Sperrnummer>();
                sperrnummern.Add(nummer);
            }
        }
    }
    else
    {
        sperrnummern = new List<Sperrnummer>();
    }

    return View(sperrnummern);
}

The resulting "sperrnummern" is always empty.

Nkosi

Given the lack of a default constructor and the readonly properties of the target class, you would have to initialize the class manually, extracting the values and passing them to the constructor arguments.

while (csv.Read()) {
    var nummer = new Sperrnummer(
        nummer: csv["Nummer"],
        erstellt: csv.GetField<DateTime>("Erstellt"),
        benutzer: csv["Benutzer"]
    );
    sperrnummern.Add(nummer);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why CsvHelper does not read data from CSV files

How to read a CSV file from SFTP and use CSVHelper to parse the content without saving CSV locally

how to use csvHelper to read the second line in a csv file

Using CsvHelper to read a CSV with headers and refer to imported data by column name

How can I retrieve data from multiple objects from a .csv file using CsvHelper?

Reading a CSV file using CsvHelper

Read all values from CSV into a List using CsvHelper

CSVHelper does not parse my Tab delimited CSV file

Issue getting cell value from CSV file using CsvHelper

CsvHelper : How to detect the Delimiter from the given csv file

Reading multiple classes from single csv file using CsvHelper

Read CSV files without Header using CSVHelper

How to avoid adding duplicate data from CSV file to SQL Server database. Using CSVHelper and C# Blazor

Write to CSV file using CsvHelper in C#

Writing into csv file display blank, using csvhelper

How to write a CSV file after reading it with CsvHelper?

How to read a header from a specific line with CsvHelper?

Why does not 'while(csv.Read())' work two / three times in CsvHelper?

CsvHelper Load DataTable from File with No Header Not Working

CSVHelper not formatting CSV

How to read uploaded CSV UTF-8 for processing with CsvHelper?

csvHelper sort dynamic data

Why my csv file doesn't pass the internal CSVHelper test?

Column sortorder when writing CSV file using CsvHelper

How to efficiently write a csv file with thousands of columns using csvhelper?

How to skip a footnote at the end of csv file using CsvHelper

CSV file Blank field error with CSVHelper C#

CSVHelper - import convert name from CSV to object with equal name property

Handling bad CSV records in CsvHelper