Microsoft
Software
Hardware
Network
Question : error saying 'External table is not in the expected format.' when trying to update database from a dataset
Hi everybody,
Iam going crazy when iam trying to import a file to updata the database thru a dataset. It was atleast working is some way till yesterday and today for some reason i dont know its showing this error every time i try to import my excel file.
Error:
--------------------------
----------
----------
----------
----------
----------
-----
External table is not in the expected format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbExc
eption: External table is not in the expected format.
Source Error:
Line 103: Dim Adapter As New OleDbDataAdapter(strQuery,
strCon)
Line 104: Dim dt As New DataTable
Line 105: Adapter.Fill(dt)
Line 106: Adapter.Dispose()
Line 107: Return dt
--------------------------
----------
----------
----------
----------
----------
--
Here is my table 'students'
id int primary key
lastname nvarchar(50)
firstname nvarchar(50)
ssn nvarchar(20)
score int
My excel file has the data for all the columns except 'id'.
Here is my code that iam using to import an excel file and trying to update the database.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, j, n1 As Integer
Dim dtTempTable As New DataTable
' readExcelSheet is a user defined function that returns me a DataTable
dtTempTable = readExcelSheet("C:\DataFil
es\Members
\" & ServerFileName, "SELECT * FROM [Sheet1$]")
Label2.Text = ServerFileName
Dim ds As New DataSet
Dim myConnection As SqlConnection = Connection() 'function that returns a sql connection
Dim strSQL As String = "SELECT firstname, lastname, ssn, score FROM students"
Label2.Text = ServerFileName & "
"
Dim myAdapter As New SqlDataAdapter(strSQL, myConnection)
Dim myCB As New SqlCommandBuilder(myAdapte
r)
Dim drTempRow As DataRow
myAdapter.SelectCommand.Co
mmandType = CommandType.Text
myCB.RefreshSchema()
myAdapter.Fill(ds, "students")
i = dtTempTable.Rows.Count()
Label2.Text &= i & " records were imported.
"
For Each drTempRow In dtTempTable.Rows()
Dim dr As DataRow = ds.Tables("students").NewR
ow()
dr("firstname") = drTempRow("firstname")
dr("lastname") = drTempRow("lastname")
dr("ssn") = drTempRow("ssn")
dr("score") = drTempRow("score")
ds.Tables("students").Rows
.Add(dr)
'ds.Tables("members").Rows
.Find(keys
() as object
'increment for the next available rec id for prospect_code
Next
'If Not ds.HasChanges(DataRowState
.Modified)
Then Exit Sub
Dim ds1 As DataSet
'ds.Tables.
'If (ds.HasChanges()) Then
If ds.HasChanges() Then
ds1 = ds.GetChanges()
myAdapter.Update(ds1, "students")
End If
myConnection.Close()
End Sub
'function that returns a datatable from an excel sheet
Public Function readExcelSheet(ByVal strExcelFileName As String, ByVal strQuery As String) As DataTable
Dim strCon As String = "Provider=Microsoft.Jet.OL
EDB.4.0;Da
ta Source=""" & strExcelFileName & """;Extended Properties=""Excel 8.0;HDR=YES;"""
Dim Adapter As New OleDbDataAdapter(strQuery,
strCon)
Dim dt As New DataTable
Adapter.Fill(dt)
Adapter.Dispose()
Return dt
End Function
Answer : error saying 'External table is not in the expected format.' when trying to update database from a dataset
When you create Excel using HTMLTextWriter, it creates Excel sheet with HTML tags. When you try to import this, you get the error you have right now. I have same problem right now. You can get by this by resaving the xls file using SaveAs. Obviously this kills the whole purpose of automating the process. You can however do SaveAs programatically. Let me know if you find any other easy way of doing this.
Thanks.
Random Solutions
Registry cannot load the hive.
Division & OLAP Cubes
CREATE AN MSDOS BOOT CD FOR ACRONIS, KNOPPIX, GHOST, WINDOWS XP REPAIR CONSOLE!
trouble with displaying recordset results in datasheet view
Same form on different computers
Outlook 2007 Continuous Authencication
XP display set to 4 bit color depth after Remote Desktop session
Error when assigning value to textbox in footer during runtime
Changing a field type while using INTO
Loop.sys