paulyoder / LinqToExcel

Use LINQ to retrieve data from spreadsheets and csv files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

System.AccessViolationException when trying to read from excel file

forReason opened this issue · comments

Hello,

I am getting the following exception:

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

This is my code:

breadcrumb.DownloadHeldCreditsList("Portfolio.xlsx");
var excel = new ExcelQueryFactory("Portfolio.xlsx");
var templist = excel.Worksheet().ToList();

it does not happen every time and i am receiving the error at line 3.
when trying manually I can open the excel file flawlessly

Are there any other exception details/stack trace? It seems strange that it only happens occasionally. Judging from your code snippet, you're downloading the file each time from somewhere? Is the file always the same in your testing, or can it change between runs?

Closing due to lack of response. Issue can be re-opened if you want to pursue this later.

Closing due to lack of response. Issue can be re-opened if you want to pursue this later.

Hello @mrworkman ,
I have a same problem when I read the .xsl file.
`An unhandled exception of type 'System.AccessViolationException' occurred in System.Data.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.`

More detail stack trace:

   at System.Data.OleDb.DataSourceWrapper.InitializeAndCreateSession(OleDbConnectionString constr, SessionWrapper& sessionWrapper)
   at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
   at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at 

If I change .xsl file to .xlsx file, I can read it normally.

You can check this link: https://stackoverflow.com/questions/29285254/oledbconnection-locks-excel-xls-file-if-the-file-is-corrupted

He has a same problem with me if he uses OleDbConnection or LinqToExcel.

Ok, thanks for the info @dang-toan. Can you share one of the files that you know causes this problem?l (link to OneDrive/Google Drive/Drop-Box.

Also, which version of Windows? Is it up to date on patches?

Which version of LinqToExcel are you using? x86, or x86_64?

Thanks

Hello @mrworkman ,
I'm so sorry to say that I cannot share you the file because it's my internal company information.
My windows: Windows 10 Enterprise - Version 1803. I just updated all components in few days ago.
LinqToExcel version is 1.11.0

And this is my code:

// Check if file is xls format -> Set engine is JET
if (fileName.ToLower().EndsWith("xls"))

            {

                excelFile.DatabaseEngine = LinqToExcel.Domain.DatabaseEngine.Jet;

Then,

IQueryable<Row> rows = from c in excelFile.Worksheet(sheetname) select c;

int count = rows.Count(); //The exception occurs here. I also try to use: rows.ToList().Count but it does not help.

If you can reproduce the problem with a new .xls file, that could be helpful too. You don't need to share private/company info 😉

@dang-toan , can you please confirm whether you are targeting 32-bit or 64-bit architecture? Jet does not work with 64-bit processes. Also, if you can, please try with the ACE database driver instead of Jet.

@mrworkman
The target is Any CPU and my system type is 64bit. Maybe it's a root cause?
And if I use ACE database driver to read .xls file, the OleDbException will occurs.
Exception of type System.Data.OleDb.OleDbException occured during excel import. External table is not in the expected format.

Let me explain more detail:
The first time, I faced the problem of OleDbException when I read .xls file by ACE database driver.
I changed my code to fix this problem with the condition: If .xls file -> read by JET. Else -> read by ACE.
I solve OleDbException problem. I can read other .xls files. But only one .xls file, I faced new problem (System.AccessViolationException).
If I try to change the .xls file to .xlsx file, I can read it without exception.