Saturday, September 6, 2008

OLE DB Connection string issue using Excel

Issue:
I came across two issues while trying to load a excel file in asp.net. I'll explain both of them separately.

1). Exception: The Microsoft Jet database engine could not find the object
This exception happens when the path is not fully qualified to the excel file used in the oledb connection string. Without path, its not going to through an exception while creating connection.

Before: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='MyFile.xls'; Extended Properties=""Excel 8.0;HDR=NO;"""

After: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='" &
Server.MapPath("MyFile.xls") & "'; Extended Properties=""Excel 8.0;HDR=NO;"""

Solution: use Server.MaptPath to get fully qualified path to a file to avoid this exception.

2). Exception: Could not find installable ISAM.
When using extended properties be careful with the syntax

Before: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='MyFile.xls'; Extended Properties=Excel 8.0;HDR=NO;"

After: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='" & Server.MapPath("MyFile.xls") & "'; Extended Properties=
""Excel 8.0;HDR=NO;"""

Solution: use quotes around the value of extended properties. If the syntax is wrong, it tries to look for another driver.

No comments: