Click here to Skip to main content
15,916,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert byte array values to datatable using csv helper.

When any null values comes means it shows the below error

The conversion cannot be performed.\r\n    Text: 'NULL'\r\n


Once it read
using (TextReader fs = new StreamReader(new MemoryStream(data)))
            {
                lines = new CsvReader(fs, csvConfig).GetRecords<T>().ToList();
            }


not having any records in lines.

How to handle this?

What I have tried:

public static DataTable ReadCSVbyteData<T>(byte[] data, string delimiter, bool hasHeaderRecord, bool hasRowCountEOF)
       {
           List<T> lines;
           var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
           {
               Delimiter = delimiter,
               HasHeaderRecord = hasHeaderRecord,
               MissingFieldFound = null
           };

           using (TextReader fs = new StreamReader(new MemoryStream(data)))
           {
               lines = new CsvReader(fs, csvConfig).GetRecords<T>().ToList();
           }

           if (hasRowCountEOF)
           {
               if (lines.Any())
                   lines.RemoveAt(lines.Count - 1);
           }

           string str = JsonConvert.SerializeObject(lines);

           return JsonConvert.DeserializeObject<DataTable>(str);
       }
Posted
Comments
Maciej Los 18-Mar-22 10:44am    
What happens if you replace this line:
Copy Code
lines = new CsvReader(fs, csvConfig).GetRecords<T>().ToList();

with:
Copy Code
using(var reader = new CsvReader(fs, csvConfig)){  var records = reader.GetRecords<T>();  lines = records.Cast<T>.ToList();}

Debug program to find out if [records] variable has data...

[EDIT]
I do believe that you use: Josh Close - CsvHelper class[^]

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