The following method of course works, but after a certain number of uploads (this is not constant) my client gets the dreaded error: "System.Data.OleDb.OleDbException: Unspecified error"
Steps:
- Client uploads an excel file via a File upload control
- File is saved to the file system
- File is opened via the oledb provider and read into a dataset
My only guess is that the provider is somehow not releasing resources.
The only way to clear this up (temporarily) is to reset IIS. For that reason, I'm inclined to think that the provider can get locked up by other websites on this server. We do host one site for a client (we did not build) that makes use of this provider, so it's possible that there is an issue on their end. Can anyone comment on this?
Please take a look at the method below and help me get rid of this problem!
Public Shared Function GetExcelData(ByVal excelFile As String, ByVal sheetNumber As Integer) As DataSet
Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", excelFile)
Dim excelDataSet As New DataSet()
Using conn As New OleDbConnection(connString)
conn.Open()
Using dt As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim excelSheets(dt.Rows.Count) As String
Dim i As Integer = 0
For Each row As DataRow In dt.Rows
excelSheets(i) = row("TABLE_NAME").ToString
i += 1
If i = sheetNumber Then
Exit For
End If
Next
Using excelCommand As New OleDbCommand("Select * from [" & excelSheets(sheetNumber - 1) & "]", conn)
Using excelAdapter As OleDbDataAdapter = New OleDbDataAdapter(excelCommand)
excelAdapter.Fill(excelDataSet)
End Using
End Using
End Using
conn.Close()
End Using
Return excelDataSet
End Function