Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got an error: There were 1 or more exceptions while reading data using type "CSVClass". Reading file "C:\Users\admin\Desktop\Project\Project\App_Data\demo.csv".

Here are my code:
C#
if (filMyFile.PostedFile != null)
{
    HttpPostedFile myFile = filMyFile.PostedFile;
    string fname = Path.GetFileName(myFile.FileName);
    string path = Server.MapPath(Path.Combine("~/App_Data/", fname));
    myFile.SaveAs(path);

    try
    {
        CsvFileDescription cv = new CsvFileDescription
        {
            SeparatorChar = ',',
            FirstLineHasColumnNames = true,
            EnforceCsvColumnAttribute = true
        };
        CsvContext cc = new CsvContext();
        List<CSVClass> csvData = (cc.Read<CSVClass>(path, cv)).ToList();

        foreach (var ss in csvData)
        {
            // Console.WriteLine(ss._strNO + "---" + ss._strCity);
        }

    }
    catch (Exception ex)
    {
    }
}

The error happen in
C#
List<CSVClass> csvData = (cc.Read<CSVClass>(path, cv)).ToList();


My class is here:
C#
public class CSVClass
{
    [CsvColumn(FieldIndex = 0)]
    public string Survey { get; set; }
    [CsvColumn(FieldIndex = 1)]
    public string Table_name { get; set; }
    [CsvColumn(FieldIndex = 2)]
    public string Code { get; set; }
    [CsvColumn(FieldIndex = 3)]
    public string YY { get; set; }
    [CsvColumn(FieldIndex = 4)]
    public string MM { get; set; }
    [CsvColumn(FieldIndex = 5)]
    public string Desc { get; set; }
    [CsvColumn(FieldIndex = 6)]
    public string Start_Value { get; set; }
    [CsvColumn(FieldIndex = 7)]
    public string End_Value { get; set; }
    [CsvColumn(FieldIndex = 8)]
    public string Remark { get; set; }
    [CsvColumn(FieldIndex = 9)]
    public bool Is_default  { get; set; }
    [CsvColumn(FieldIndex = 10)]
    public int Order_no { get; set; }
    [CsvColumn(FieldIndex = 11)]
    public int? ParentId { get; set; }
    [CsvColumn(FieldIndex = 12)]
    public string Parent_Table { get; set; }
}


My csv:
Survey,Table_name,Code,YY,MM,Desc,Start_Value,End_Value,Remark,Is_default,Order_no,ParentId,Parent_Table
SEV,sevcodetable,00,2015,04,test,,,,1,1,,


What does it mean more exceptions? how can I fix it?

Thanks
Posted
Updated 29-Sep-21 6:43am
v3
Comments
Maciej Los 29-Jul-15 6:45am    
Not enough information! Please improve your qestion and add class definition.
Kornfeld Eliyahu Peter 29-Jul-15 7:16am    
Maybe the data not CSV after all...
[no name] 30-Jul-15 0:23am    
Stop ignoring the exceptions that you are getting would be a good start.
Iris Shing 30-Jul-15 0:29am    
Sorry, I m not really understand "Stop ignoring the exceptions", I only get this exception
[no name] 30-Jul-15 1:28am    
How would you know? You have an empty catch block so you are ignoring any exception that you are getting.

Read From a CSV File


C#
public List<string[]> DelimeterFileToList(string strFileName,string[] aDelimeters)
{
            List<string[]> lstData = new List<string[]>();
            try
            {
                using (TextFieldParser parser = new TextFieldParser(strFileName))
                {
                    parser.Delimiters = aDelimeters;
                    while (true)
                    {
                        string[] parts = parser.ReadFields();
                        if (parts == null)
                        {
                            break;
                        }
                        lstData.Add(parts);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log Exception
            }          
   return lstData;
}

Write to a CSV from List


C#
public WriteListToCSV(List<CSVClass> lstCsvClass,string FileName)
{
    var engine = new FileHelperEngine(typeof(CSVClass))
    {       
       HeaderText = typeof(CSVClass).GetCsvHeader()
    };
    if (!Directory.Exists(path.GetDirectory(FileName)))
    {
        Directory.CreateDirectory(path.GetDirectory(FileName));
    }     
       engine.WriteFile(FileName, lstCsvClass.ToArray());
       lstCsvClass.Clear();
}

Name spaces Required :


C#
using FileHelpers;
using FileHelpers.RunTime;
 
Share this answer
 
v2
It's an aggregated exception type, usually a busted column type,if you breakpoint the exception check the ineer exceptions list, you will find for example a list of errors like this:

{"Value \"0\" in line 2 has the wrong format for field or property \"ShowOnSite\" in type \"CoreWebFeed.DAL.BundleCSVProcessor\". Reading file \"C:\\Users\\jholden\\source\\repos\\CoreWebFeed\\CoreWebFeed\\DataLoading\\Bundles.csv\"."}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900