Thursday, February 14, 2008

Interesting Data Type of DataColumn generated from Excel data source

I generated a DataTable from an excel file by using ADO.NET, which the excel file includes two columns "notes","id". like:


Excel file:
ID        NOTES (ignore the rest of columns ...)
201      FunnytEST
202      78
203      Interesingtest2
204      90
205      67
...



After I got my DataTable, I wrote up a piece of codes to loop through the 'NOTES' column, and find out something which is quite interesting!

In this case above, apart from 78,90,67, supprisingly I got 2 NULLs instead of "FunnytEST" and "Interesingtest2"! and the DataTable.Column("NOTES").DataType is equal to String.Double

and then I changed the data of excel file to :


ID         NOTES (ignore the rest of columns ...)
201      FunnytEST
202      ChangeNumberToString
203      Interesingtest2
204      90
205     67


then the DataTable.Column("NOTES").DataType was changed to System.String the last two cells (90 and 67) of column NOTES can not be read.

OK, that makes sence, because if the data type of that column has been declared with System.String, so any data in Double or Int can not be read.

it seems like ADO.Net creates data type for DataColumn based on how many cells in Double type that column have and how many cells in String type. After comparaison, the ADO.NET chooses the one that is used mostly by cells to be the final data type for that column.(do you know wat I try to explain? ... well.. in fact, I don't know wat I talk about :-))


And so that must be something wrong with the excel file itself then. I opened the excel file right click NOTES column ->choose Cell Format, on Category changed General to Text, click OK.

Now tried again. Problem solved!


PS:
This bloody problem caused my fucking whole day (and my lovely lunch hour!) on it. Fuck!

Labels: , , ,

0 Comments:

Post a Comment

<< Home