locked
Show csv data in view RRS feed

  • Question

  • User-2063712806 posted

    Dear all,

    I recently started using csvhelper. But at the moment I am facing an issue.
    As you probably have read the title, the datetime wont work.

    Sources:

    https://joshclose.github.io/CsvHelper/ & https://github.com/JoshClose/CsvHelper/issues/1123 


    This is my code:

    Controller:
    public IActionResult _GetOrganisationInfo()
    {
    TextReader reader = new StreamReader(@"CSV/Organisation.csv");

             var csvReader = new CsvReader(reader);
            csvReader.Configuration.HasHeaderRecord = false;
            csvReader.Configuration.RegisterClassMap<OrganisationMap>();
            
            var records = csvReader.GetRecords<Organisation>();
            while (csvReader.Read())
            {
               
                var record = csvReader.GetRecord<Organisation>();
            }
    
            return PartialView(@"~/Views/Shared/DivTest.cshtml", reader);
        }
    

    Class:
    public class Organisation
    {
    public int ORG_ID { get; set; }
    public int ORG_NO { get; set; }
    public int ORG_TYPE_ID { get; set; }
    public string ORG_NAME { get; set; }
    public DateTime LAST_MODIFIED { get; set; }
    }

    public class OrganisationMap: ClassMap<Organisation>
    {
        public OrganisationMap()
        {
            Map(m => m.LAST_MODIFIED).TypeConverterOption.Format("yyyy-MM-dd HH:mm:ss.fff");
        }
    }
    

    An example of what 1 of my csv documents looks like:
    166,1,4,'276 16 062 055 0050','2017-12-07 16:25:32.217'

    I get my error at the:
    while (csvReader.Read())
    {
    var record = csvReader.GetRecord();
    }

    The error I get:

    CsvHelper.ReaderException: 'An unexected error occured.'

    Inner Exception FormatException: String was not recognized as a valid DateTime

    The result I want:

    To remove the error and display the document in my view. So hopefully is someone able to help me with this.  If there is code to do this better please attach it.

    Kind regards.

    Thursday, September 20, 2018 2:12 PM

Answers

All replies

  • User-2063712806 posted

    Hello,

    After a bit more trying I have removed the error that the string was not recognized as DateTime.

    My current controller code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using System.Web;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using App.Models;
    using System.IO;
    using CsvHelper;
    using System.Globalization;
    
    public IActionResult _GetOrganisationInfo()
    {
    List<Organisation> ShowInfo = new List<Organisation>();        
    TextReader reader = new StreamReader(@"CSV/Organisation.csv"); 
    var csv = new CsvReader(reader);                               
    csv.Configuration.HasHeaderRecord = false;                     
    csv.Configuration.RegisterClassMap<OrganisationMap>();         
    csv.Read();                                                    
    var records = csv.GetRecords<Organisation>().ToList();         
    
    return PartialView(@"~/Views/Shared/DivTest.cshtml", ShowInfo);
    }
    

    My new requirement:

    Load the data in view.

    I currently have an h1 and a foreach. When I click the button it only loads the h1 and not the csv data.

    Note: 

    My model is still the same.

    My partial view code:

    @using App.Models;
    @model List<Organisation>;
    
    <h1>info</h1>
    
    @foreach (var ShowInfo in Model) 
    {
        @ShowInfo.ORGANISATION_ID;
    
    }

    Hopefully is osmeone able to fix this. Regards.

    Friday, September 21, 2018 7:12 AM
  • User-1764593085 posted

    Hi bjeako,

    You do not set the ShowInfo? try to replace

    csv.Read();
    var records = csv.GetRecords<Organisation>().ToList();

    to below code:

    ShowInfo = csv.GetRecords<Organisation>().ToList();

    And in your partial view:

    @using App.Models;
    @model IEnumerable<Organisation> <h1>info</h1> @foreach (var ShowInfo in Model) { <h1>@ShowInfo.ORG_ID</h1> }

    Xing

    Friday, September 21, 2018 9:21 AM
  • User-2063712806 posted

    Dear Xing,

    I have tried your suggestion. But, now I get the error FormatException: String was not recognized as a valid DateTime. at line

    ShowInfo = csv.GetRecords<Organisation>().ToList();

    This is my current code now:

    My csv file is still the same.

    Class:

    public class Organisation
        {
            public int ORG_ID { get; set; }
            public int ORG_NO { get; set; }
            public int ORG_TYPE_ID { get; set; }
            public string ORG_NAME { get; set; }
            public DateTime LAST_MODIFIED { get; set; }
    
        }
    
        public class OrganisationMap: ClassMap<Organisation>
        {
            public OrganisationMap()
            {
                Map(m => m.LAST_MODIFIED).Default("yyyy-MM-dd HH:mm:ss.fff");
            }
        }

    Controller:

    public IActionResult _GetOrganisationInfo()
    {
    List<Organisation> ShowInfo = new List<Organisation>();        
    TextReader reader = new StreamReader(@"CSV/Organisation.csv"); 
    var csv = new CsvReader(reader);                               
    csv.Configuration.HasHeaderRecord = false;                     
    ShowInfo = csv.GetRecords<Organisation>().ToList();            
    csv.Configuration.RegisterClassMap<OrganisationMap>();         
    csv.Configuration.Delimiter = ",";
                                                                   
    return PartialView(@"~/Views/Shared/DivTest.cshtml", ShowInfo);
    }

    View: 

    @using SLM_App.Models;
    @model IEnumerable<Organisation>
    
    <h1>info</h1>
    
    @foreach (var ShowInfo in Model)
    {
        <h1>@ShowInfo.ORGANISATION_ID</h1>
    
    }

    I hope you can help me with this.

    Kind regards

    Friday, September 21, 2018 10:04 AM
  • User-1764593085 posted

    Hi bjeako,

    My suggestion is that you could change your csv data to below format without using ' ', it may be a workaround:

    166,1,4,'276 16 062 055 0050',2017-12-07 16:25:32.217

    The default built in converters will handle most cases of type conversion where it should be able to convert the type of the properties of your class.

    You could refer to below link to learn how to convert string to DateTime:

    https://stackoverflow.com/questions/35990627/csvhelper-parse-csv-and-convert-string-to-datetime/35991019#35991019

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 21, 2018 11:18 AM
  • User-2063712806 posted

    Dear Xing,

    By removing the ' ' from the datetime does work!  Only the problem now is that the documents I have, still have those ' '. So the task for me will be now to figure out how to set the ' ' hidden without removing the ' ' in the csv files.

    Still a HUGE thank you for me helping me!

    Kind regards,

    Bjeako

    Friday, September 21, 2018 11:39 AM