Returns Excel in C# List is returning null

Asked

Viewed 95 times

2

This code reads a file Excel and plays a list, the problem that is occurring is it is returning null, like there’s nothing in the Excel, but the column names are correct:

string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + local + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;\";";
System.Data.OleDb.OleDbConnection conn = new OleDbConnection(PathConn);
string sqlCommand = "Select * From [Plan1$]";
OleDbCommand command = new OleDbCommand(sqlCommand, conn);
List<Entidades> listaComentario = new List<Entidades>();

try
{
    conn.Open();
    OleDbDataReader rd = command.ExecuteReader();

    while (rd.Read())
    {
        listaComentario.Add(new Entidades()
        {
            PNR = rd[System.Configuration.ConfigurationManager.AppSettings["PNR"]].ToString(),
            Status = rd[System.Configuration.ConfigurationManager.AppSettings["Status"]].ToString(),
        });
    }

    if (listaComentario.Count() > 0)
        return listaComentario;
    else
        return null;

}
  • You tried to thresh? You know if he’s not even in while? Try to isolate the problem further.

  • Remove the Try catch block to check the error better. Then you can return it.

1 answer

2


just for the record. I found the problem. I switched my machine with one that works with a 64-bit processor. The error, was falling on Catch, when debugged, I found the problem.

I was returning "microsoft.ace.oledb.12. 0 not registrered on the local machine". To fix, it was enough to install a 64-bit Data Connectivity Driver, which I picked up on the Microsoft website: http://www.microsoft.com/en-us/download/details.aspx?id=23734,

Thank you to all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.