I'm building a Windows Forms app which reads an Excel 2013 file (*.xlsx). This file contains data structured like a table. The method I use to read data from this file is described here:
How to read data from excel file using c# [duplicate]
This method uses an OleDbConnection
object along with a connection string like connecting to a database.
Also uses an OleDbCommand
object to supply a query to retrieve data.
And also uses an OleDbDataReader
object to read the retrieved data.
The problem is that when I read some particular rows of this Excel file, the OleDbDataReader
object is retrieving in some columns the correct value, but in one column in particular is retrieving a System.DBNull
value when there is really data in that column!!!
Example:
- My Excel file:
ClientId ClientName Total InvoiceNumber
---------------------------------------------------
...
4 CLIENT_4 400.00 EV1234
5 CLIENT_5 500.00 EV56.78
6 CLIENT_6 600.00 EV9012
...
Debbugging my app, line by line, adding an expression in "Inspection 1" window, when I reach row 4 for "CLIENT_4": the values are retrieving correctly:
For column ClientId
==> Data is 4.0
and type is object{double}
For column ClientName
==> Data is CLIENT_4
and type is object{String}
For column Total
==> Data is 400.00
and type is object{decimal}
For column InvoiceNumber
==> Data is EV1234
and type is object{double}
But when I reach row 5 for "CLIENT_5": the InvoiceNumber
column value is retrieving incorrectly:
For column ClientId
==> Data is 5.0
and type is object{double}
For column ClientName
==> Data is CLIENT_5
and type is object{String}
For column Total
==> Data is 500.00
and type is object{decimal}
For column InvoiceNumber
==> Data is {}
and type is object{System.DBNull}
when column value in the Excel file is really "EV56.78"
This is happening for every column with the same pattern (A string with numbers and a decimal period).
1. Why OleDbDataReader
object is taking this column value for a NULL when value is like this pattern instead of reading it like a String
??? Is decimal period messing with data type when the datareader retrieves it???
2. Is cell formatting from my Excel file messing with data type??? The whole column and all cells in column "InvoiceNumber" have "General" format.
3. Do I have to add an extra parameter to OleDbDataReader
object for parsing column values or something similar???
I'm developing on Visual Studio 2010 Ultimate Edition;
64bit O.S.; reading an Excel file from MS-Office 2013.
If you need extra information or code snippets, I'll edit the question to explain myself more (I think this info is fairly enough).
Thanks in advice for any help if you faced something similar to this issue!!!