Coder Social home page Coder Social logo

Comments (14)

JoshClose avatar JoshClose commented on June 2, 2024

Can you paste in a small (couple of rows) example where the issue occurs?

from csvhelper.

vk5880 avatar vk5880 commented on June 2, 2024

Hi, here is some additioinal information:

CsvHelper.1.8.0\lib\net40-client\CsvHelper.dll
Runtime version v4.0.30319
Version 1.8.0.22497

public class UserCsvModel
{
public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Email { get; set; }
}

CsvFile contents inside []
[Id,FirstName,LastName,Email
1,v,k,[email protected],
2,v,k,[email protected],]

from csvhelper.

JoshClose avatar JoshClose commented on June 2, 2024

Did you use CsvWriter to create the contents of the file? How did the extra comma get on the end of the rows? What error are you seeing? What do you mean by manually modified and removed empty row at end?

from csvhelper.

vk5880 avatar vk5880 commented on June 2, 2024

Sorry, my mistake, I incorrectly removed unnecessary columns in last example.
CsvFile contents inside []
[Id,FirstName,LastName,Email,Address
1,v,k,[email protected],Some address
2,v,k,[email protected],]

An error occurred trying to read a record of type 'UserCsvModel'.
Row: '4' (1-based)
Field Index: '0' (0-based)
Field Name: 'Id'
Field Value: ''

System.Exception: is not a valid value for Int32. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFromString(String text)
at lambda_method(Closure , ICsvReader )
at CsvHelper.CsvReader.d__0`1.MoveNext() in C:\Projects\CsvHelper\src\CsvHelper\CsvReader.cs:line 585

Just try to write a csv file, remove last \r\n characters and then try to read it. there should be no line end on last row.

from csvhelper.

JoshClose avatar JoshClose commented on June 2, 2024

I'm not able to reproduce it based on this information. I tried what you suggested and it's working for me, so I must be missing something.

Can you create a small example console app, zip it up, and email it to me?

from csvhelper.

vk5880 avatar vk5880 commented on June 2, 2024

Paste this in CsvReaderTests.cs

           class UserCsvModel { 
        public int Id { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
        public string Email { get; set; }
        public string Address { get; set; }
    }

    [Fact]
    public void GetRecordsTest2()
    {
        var badCsv = @"Id,FirstName,LastName,Email,Address

1,v,k,[email protected],Some address
2,v,k,[email protected],1245";

        var stringReader = new StringReader(badCsv);
        var csvReader = new CsvReader(stringReader);            
        var records = csvReader.GetRecords<UserCsvModel>().ToList();

        Assert.Equal( 2, records.Count );

        for( var i = 1; i <= records.Count; i++ )
        {
            var record = records[i - 1] as UserCsvModel;
            Assert.Equal( i, record.Id );
        }
    }

from csvhelper.

vk5880 avatar vk5880 commented on June 2, 2024

Have you succeeded with exception reproduction?

from csvhelper.

JoshClose avatar JoshClose commented on June 2, 2024

Sorry, been busy. I'm looking into it now.

from csvhelper.

vk5880 avatar vk5880 commented on June 2, 2024

I think it is a bad practice to use the same class variable between method calls and support it state. It is better to use method argument in this case and take advantage of method scope. You have char c variable inside CsvParser and you probably forgot to clean it in the beginning of ReadLine method. This produce problem.

from csvhelper.

JoshClose avatar JoshClose commented on June 2, 2024

That's pretty much the case. I'll have a fix committed in a minute here and I'll link the change set.

from csvhelper.

JoshClose avatar JoshClose commented on June 2, 2024

Are you saying that the char c variable should be passed into Read()?

from csvhelper.

JoshClose avatar JoshClose commented on June 2, 2024

It's committed. Essentially a one line fix. ffbbada

from csvhelper.

vk5880 avatar vk5880 commented on June 2, 2024

When we start to read next row do we really need to know what last character was? I may be wrong, but it seems to me that c can be local variable of ReadLine().
Thank you.

11.10.2012, 06:09, "Josh Close" [email protected]:

Are you saying that the char c variable should be passed into Read()?


Reply to this email directly or view it on GitHub.

Kind Regards, Костенко Владислав.

from csvhelper.

JoshClose avatar JoshClose commented on June 2, 2024

I will take a look.

from csvhelper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.