Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting FieldHeaders #737

Closed
Codehhh opened this issue Aug 25, 2017 · 15 comments
Closed

Getting FieldHeaders #737

Codehhh opened this issue Aug 25, 2017 · 15 comments

Comments

@Codehhh
Copy link

Codehhh commented Aug 25, 2017

I just started using your package today but I can't figure out how to easily get the FieldHeaders.

I see some issues that include csv.FieldHeaders but I can't seem to find that in the code or any documentation. What is the simplest way for me to get the FieldHeaders using your library?

I'm currently using v3.0.0-chi06 and netcore v2.0.0

Thanks.

@jamesbascle
Copy link
Contributor

jamesbascle commented Aug 25, 2017 via email

@Codehhh
Copy link
Author

Codehhh commented Aug 25, 2017

@jamesbascle

Sorry, I forgot to include the version I was using in my original post but I ninja edited after realizing.

I'm using v3.0.0-chi06.

My code currently looks like the following:

var csv = new CsvReader( textReader );
csv.Read();
csv.ReadHeader();
var header = csv.FieldHeaders; // Doesn't exist?
var records = csv.GetRecords<MyClass>();

@JoshClose
Copy link
Owner

var header = csv.Context.HeaderRecord;

@Codehhh
Copy link
Author

Codehhh commented Aug 25, 2017

Thank you!

@JoshClose
Copy link
Owner

No problem!

@leedavi
Copy link

leedavi commented Sep 10, 2019

This solution doesn't work for me. It errors with:

{"Method not found: 'CsvHelper.ReadingContext CsvHelper.CsvReader.get_Context()'."} | System.MissingMethodException

I've using the latest version fromthe nuGet package.

Is there another way to get the header data?

@leedavi
Copy link

leedavi commented Sep 10, 2019

I've had to hardcode the header data to get this to work.

please get back to me. This is the first time I've used csvhelper, and it looks great, but failing to access header data is a big problem. The header data is in the FieldHeaders list, but not worth much if we cannot access it.

@AltruCoder
Copy link

I have tried the following and it works just fine for me with the current version of CsvHelper.

var csv = new CsvReader( textReader );
csv.Read();
csv.ReadHeader();
var header = csv.Context.HeaderRecord;
var records = csv.GetRecords<MyClass>();

@leedavi
Copy link

leedavi commented Sep 10, 2019

Thx for getting back to me, this is strange I tried the code below and it caused an error.

                    using (TextReader reader = File.OpenText(fnamePath))
                    {
                        var lp = 1;
                        CsvReader csv = new CsvReader(reader);

csv.ReadHeader();
var header = csv.Context.HeaderRecord;

                        while (csv.Read())
                        {
                            ImportCSV Record = new ImportCSV();
                            Record.FilterStatus = csv.GetField(0);
                            Record.FilterSysteem = csv.GetField(1);
                            Record.FilterType = csv.GetField(2);
                            Record.FilterKleur = csv.GetField(3);
                            Record.Category = csv.GetField(4);
                            Record.ProductName = csv.GetField(5);
                            Record.Model = csv.GetField(6);
                            Record.LegancyItemId = csv.GetField(7);
                            Record.Code = csv.GetField(8);
                            Record.Price = csv.GetField(9);
                            Record.Discount = csv.GetField(10);
                            Record.NetPrice = csv.GetField(11);
                            importList.Add(Record);
                        }
                        lp += 1;
                    }

Do you have any idea why?

@AltruCoder
Copy link

AltruCoder commented Sep 10, 2019

You need csv.Read(); before csv.ReadHeader();. Also what error are you getting?

@leedavi
Copy link

leedavi commented Sep 10, 2019

Ah!!! OK thx, I assumed the read header was independent or the read. I'll give this a try. Thx for your help.

@leedavi
Copy link

leedavi commented Sep 10, 2019

The error was written above:

{"Method not found: 'CsvHelper.ReadingContext CsvHelper.CsvReader.get_Context()'."} | System.MissingMethodException

But I'll give the read before a try and let you know what happens.

@AltruCoder
Copy link

Your issue may have to do with conflicting versions in your project. stackoverflow

A MissingMethodException almost always means that your application is running using a different version of an assembly than the assembly used to compile it against.

In this case it happened because another project, referenced by your main application, used an older version of the CsvHelper library, where that property wasn't present and thus its getter method wasn't found.

Make sure all projects in your solution reference the same version of the library, so its assemblies won't get overwritten with an older version on build.

@leedavi
Copy link

leedavi commented Sep 10, 2019

OK, I'm running this in a DNN CMS. So I suppose it's possible, but I'm not aware of DNN using this, in fact I'm fairly sure it doesn't. But maybe I've overwritten an older version added by a module that was installed. I'll investigate. Thanks Again.

@leedavi
Copy link

leedavi commented Sep 12, 2019

You are correct. There is a module on the site which installs v2, I should have seen this before.

Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants