Importing .csv in ASP.Net Does Not Open File

Hello misters! 
Can you help me out, please?

I’m having an ASP.Net site from a company that has a problem importing .csv files into the platform, it does not recognize the contents of the file. For files in .txt is working normally, only .csv that does not open. Could you help me?

code below…

public static DataTable CarreguePlanilha(string caminho)
var dtComColunas = new DataTable();
var criptografado = Path.GetExtension(Path.GetFileName(caminho)) == “.emailing”;

//se é csv…
if ((new string { “.csv”, “.txt” }).Contains(Path.GetExtension(Path.GetFileName(caminho))))
var csv = new CsvReader(new StreamReader(caminho), new CsvHelper.Configuration.Configuration { Delimiter = “;” });


foreach (var h in csv.Context.HeaderRecord)
dtComColunas.Columns.Add(h, typeof(string));

dtComColunas.Rows.Add(csv.Context.HeaderRecord.Select(h => csv.GetField<string>(h)).ToArray());

while (csv.Read())
dtComColunas.Rows.Add(csv.Context.HeaderRecord.Select(h => csv.GetField<string>(h)).ToArray());

return dtComColunas;

using (var conexao = new OleDbConnection(string.Format(“Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};” + "Extended Properties=“Excel 12.0 xml;HDR=NO;IMEX=1"”, caminho)))
//Como OLE insiste em ignorar o IMEX=1 em algumas situações, os headers das colunas são importados como parte dos dados para forçar o tipo texto. Então depois é preciso acertar as colunas
var tabela = conexao.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object { null, null, null, “TABLE” }).Rows[0][“TABLE_NAME”].ToString();
var aes = new RijndaelManaged().CreateDecryptor(new UnicodeEncoding().GetBytes(“8560739”), new byte { 95, 36, 10, 29, 94, 183, 74, 222, 186, 65, 161, 248, 177, 198, 148, 11 });
var sql = new OleDbCommand(string.Format("select * from [{0}] ", tabela), conexao);
var dt = new DataTable();

new OleDbDataAdapter(sql).Fill(dt);

if (dt.Rows.Count == 0) return dt;

var row0 = dt.Rows[0];

foreach (DataColumn c in dt.Columns)
dtComColunas.Columns.Add(row0[c.ColumnName].ToString(), typeof(string));


foreach (DataRow dr in dt.Rows)
if (criptografado)
for (var i = 0; i < dt.Columns.Count; ++i)
using (var fonteMemoria = new MemoryStream(Convert.FromBase64String((string)dr[i])))
using (var fonteCriptografica = new CryptoStream(fonteMemoria, aes, CryptoStreamMode.Read))
using (var fonteLeitora = new StreamReader(fonteCriptografica))
var a = fonteLeitora.ReadToEnd();
dr[i] = a;


return dtComColunas;


Exception User-unhandled
System.NullReferenceException: ‘Referência de objeto não definida para uma instância de um objeto.’
<Temporary local of type ‘string’> was null.


You don’t say, but it _looks_ like you are using the CsvHelper project. If that is not the case, ignore everything else here …

Have you tried posting your question to their Github repository?

The null exception at csv.Context.HeaderRecord suggests that either the csv file doesn’t have a header, or you failed to read it. Perhaps you need to call csv.ReadHeader(), as shown in this example ?  In any case, you should probably check to see if HeaderRecord is null before attempting to use it.

1 Like