1

I'm trying to write a program that loads a CSV file into a custom class I've created. To do this I'm using the following library: http://joshclose.github.io/CsvHelper/#mapping

I've written my entity class, as follows:

public class BreakthroughCampaignEntity
{
    public string ConstituentId { get; set; }
    public string F2F { get; set; }
    public string GiftStatus { get; set; }
    public string Title1 { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public string PreferredAddress { get; set; }
    public string PreferredSuburb { get; set; }
    public string PreferredEmailNumber { get; set; }
    public string PreferrredHomeNumber { get; set; }
    public string PreferredWorkNumber { get; set; }
    public string PreferrredMobileNumber { get; set; }
    public string GiftAmount { get; set; }
    public DateTime GiftNextTransactionDueDate { get; set; }
    public DateTime GiftSpecificAttributesPledgeGiftSignUpDate { get; set; }
}

And my index mapping:

public class BreakthroughCampaignIndexMap : CsvClassMap<BreakthroughCampaignEntity>
{
    /// <summary>
    /// Creates a map by matching the CSV indexes to our entity class
    /// </summary>
    public override void CreateMap()
    {
        Map(m => m.ConstituentId).Index(0).Default("Unknown");
        Map(m => m.F2F).Index(1).Default("Unknown");
        Map(m => m.GiftStatus).Index(2).Default("Unknown");
        Map(m => m.Title1).Index(3).Default("Unknown");
        Map(m => m.FirstName).Index(4).Default("Unknown");
        Map(m => m.Surname).Index(5).Default("Unknown");
        Map(m => m.PreferredAddress).Index(6).Default("Unknown");
        Map(m => m.PreferredSuburb).Index(7).Default("Unknown");
        Map(m => m.PreferredEmailNumber).Index(8).Default("Unknown");
        Map(m => m.PreferrredHomeNumber).Index(9).Default("Unknown");
        Map(m => m.PreferredWorkNumber).Index(10).Default("Unknown");
        Map(m => m.PreferrredMobileNumber).Index(11).Default("Unknown");
        Map(m => m.GiftAmount).Index(12).Default("Unknown");
        Map(m => m.GiftNextTransactionDueDate).Index(13);
        Map(m => m.GiftSpecificAttributesPledgeGiftSignUpDate).Index(14);
    }
}

I've also installed the CSV helper library via the package manager console, to ensure I have the latest version.

Unfortunately however, when I go to load my CSV, using the following:

var csv = new CsvReader(TextFileToProcess.Text.ToString());

var processedFile = csv.GetRecords<BreakthroughCampaignEntity>();

As referenced in this StackExchange post (answer by Josh Close) I receive the following error below TextFileToProcess.Text.ToString():

Cannot resolve constructor 'CsvReader(string)', candidates are: CsvReader(CsvHelper.ICsvParser) (in class CsvReader) CsvReader(System.IO.TextReader) (in class CsvReader)

I've also tried using:

var csv = new CsvReader(File.OpenRead(TextFileToProcess.Text.ToString()));

Which provides the same error.

I'd love to understand what this means, not just how to fix it! I have some gaps in my knowledge which means although this error seems to indicate how to fix the problem, I don't understand what it's telling me.

2 Answers 2

4

The error message means there is no constructor for CsvReader which takes the csv text as an argument. The error then tells you what constructors are available to help you figure out the correct one.

This one takes an instance of ICsvParser:

CsvReader(CsvHelper.ICsvParser) (in class CsvReader)

This one is probably your best option and takes a TextReader.

CsvReader(System.IO.TextReader) (in class CsvReader)

If you have the filename you could do this:

new CsvReader(File.OpenText(filePath));

If you just have the csv text you can create a StringReader:

new CsvReader(StringReader(TextFileToProcess.Text.ToString()));
Sign up to request clarification or add additional context in comments.

Comments

2

The parameter for the second constructor of CsvReader that is shown in the error message has type System.IO.TextReader. Therefore, I think you should be able to get it to work with this one:

var csv = new CsvReader(File.OpenText(filePath));

where filePath is a string containing the path of the file that you're opening. It seems that you're trying to give the constructor a parameter that is a string that is the text of the file, but it doesn't seem to have a constructor for that.

Look at the example in the documentation for TextReader class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.