Nandan hegde
Nandan hegde

Reputation: 61

ClosedXML Read excel file Error: The type initializer for 'MS.Utility.EventTrace' threw an exception. (C#)

I was developing Excel upload functionality in the asp.net application using closedXML. The functionality works fine when I use local IIS and gives exception when I use Visual Studio 2013. Which is not allowing me to do further debugging in the code.

Exception:

Message: The type initializer for 'MS.Utility.EventTrace' threw an exception.
Stack Trace: 
   at MS.Utility.EventTrace.EasyTraceEvent(Keyword keywords, Event eventID)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, Boolean streaming)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable)
   at ClosedXML.Excel.XLWorkbook.LoadSheets(String fileName)
   at ClosedXML.Excel.XLWorkbook.Load(String file)
   at ClosedXML.Excel.XLWorkbook..ctor(String file, XLEventTracking eventTracking)
   at ClosedXML.Excel.XLWorkbook..ctor(String file)
   at OnlinePolicyUpload.ReadExcel(String filePath) in d:\Application\XYZ\OnlineUpload.aspx.cs:line 413

Inner exception:

Message: Requested registry access is not allowed.
Stack Trace:    
   at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
   at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
   at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
   at Microsoft.Win32.Registry.GetValue(String keyName, String valueName, Object defaultValue)
   at MS.Utility.EventTrace.IsClassicETWRegistryEnabled()
   at MS.Utility.EventTrace..cctor()

My Code segment:

private DataTable ReadExcel(string filePath)
    {
        try
        {
            DataTable dt = new DataTable();
            IXLWorkbook workBook = new XLWorkbook(filePath);
            IXLWorksheet workSheet = workBook.Worksheet(1);
            bool firstRow = true;
            foreach (IXLRow row in workSheet.Rows())
            {
                if (firstRow)
                {
                    foreach (IXLCell cell in row.Cells())
                    {
                        dt.Columns.Add(cell.Value.ToString());
                    }
                    firstRow = false;
                }
                else
                {
                    dt.Rows.Add();
                    int i = 0;
                    foreach (IXLCell cell in row.Cells())
                    {
                        dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
                        i++;
                    }
                }
            }
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Exception is thrown at line IXLWorkbook workBook = new XLWorkbook(filePath);

File path has Full access permission to Everyone, IIS_IUSRS. Changing this to false will affect my application login to fail. <identity impersonate="true" userName="username" password="pass" />

Tried changing permission to everyone with full access to registry HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics. No luck.

Please help me with this.

Upvotes: 0

Views: 5370

Answers (1)

Nandan hegde
Nandan hegde

Reputation: 61

It was a really old application developed in 2008. The issue is fixed by properly updating CrystalDecisions.Web.dll

Upvotes: 1

Related Questions