1

I am getting The directory is not empty exception from my code only when I deploy it on the server.

Debugging through the exception found that there is some glitch with the excelReader.Close();

Code is as follow

 IExcelDataReader excelReader = null;    
 DeleteFile(path);
 postedFile.SaveAs(path);

 FileStream stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
 if (Extension.ToLower() == ".xlsx")
 {
      excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
 }
 else if (Extension.ToLower() == ".xls")
 {
      excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
 }
 excelReader.IsFirstRowAsColumnNames = true;

 DataSet result = excelReader.AsDataSet();

 if (result.Tables.Count > 0)
 {
      dt = result.Tables[0];
 }
 stream.Close();
 stream.Dispose();
 DeleteFile(path);
 if (excelReader != null)
 {
      File.AppendAllText(
         @"D:\Websites\registry.aan.com\OldAuditLogs\Error.txt",
        "excelReader if");

      excelReader.Close();
      excelReader.Dispose();
 }
 else {
     File.AppendAllText(
        @"D:\Websites\registry.aan.com\OldAuditLogs\Error.txt",
        "excelReader else");
 }

 File.AppendAllText(
   @"D:\Websites\registry.aan.com\OldAuditLogs\Error.txt",
   "excelReader out");

 result.Dispose();
Esko
  • 4,109
  • 2
  • 22
  • 37
Niks
  • 110
  • 1
  • 1
  • 10
  • if you change `excelReader.Close()` to `if (excelReader != null) { excelReader.Close() }` does the error go away? – John Smith Jun 24 '16 at 02:23
  • 1
    why are you deleting the folder? shouldn't you delete just the file? – muratgu Jun 24 '16 at 04:07
  • Hello iliketocode, thanks for your quick response, I have modified the code as you suggested but i still get the same exception. the code runs with no flaws on my local machine, but when i deploy the same on the server it get the exception. I have modified the code as above. – Niks Jun 24 '16 at 06:47
  • Hello muratgu, thanks for your quick response, i have removed the section for folder deletion. – Niks Jun 24 '16 at 06:51
  • I'm having the same issue. Did you find a resolution? – user1651370 Dec 13 '18 at 09:41
  • Upgrading the package from 2.1.1 to 3.4.2 fixed the problem for me – user1651370 Dec 13 '18 at 11:23

1 Answers1

0

Upgrade ExcelDataReader to version 3.4.2

user1651370
  • 863
  • 1
  • 8
  • 15