J
jef
I'm attempting to use OLEDB to write an Excel file. I start with an
existing file that has a single worksheet with one row (the column
headers). Everything seems to be working except when I try to save the
file I get the above mentioned error. I'm running on Windows 2003
Server. I'm able to write a text file to the same folder I'm writing
the Excel file... and the permissions on the XLS file are read/write.
Here's my code:
Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=ExportFile.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim oConn As New OleDbConnection(sConn)
System.IO.File.CreateText("blah.txt")
'Get the list of tables (worksheets) from the Excel file
oConn.Open()
Dim schemaTable As DataTable =
oConn.GetOleDbSchemaTable(Data.OleDb.OleDbSchemaGuid.Tables, New
Object() {Nothing, Nothing, Nothing, "TABLE"})
Dim sTable As String
'There should only be one table (worksheet) in the file
'If schemaTable.Rows.Count = 1 Then
sTable = schemaTable.Rows(0)!TABLE_NAME.ToString
Dim objCmd As New OleDbCommand("SELECT * FROM [BBSC - SEND$]", oConn)
Dim objDA As New System.Data.OleDb.OleDbDataAdapter()
objDA.SelectCommand = objCmd
' Fill DataSet
Dim objDS As New DataSet()
objDA.Fill(objDS)
Dim objInsCmd As OleDbCommand = New OleDbCommand( _
"INSERT INTO [BBSC - SEND$] (BBSC_TRANSACTION_ID,
BBSC_IMAGE_ID, BBSC_RECEIVED_DATE, BBSC_SEND_DATE) " & _
"VALUES (?, ?, ?, ?)", oConn)
objInsCmd.Parameters.Add( _
"BBSC_TRANSACTION_ID", OleDbType.VarChar, 5, "BBSC_TRANSACTION_ID")
objInsCmd.Parameters.Add( _
"BBSC_IMAGE_ID", OleDbType.VarChar, 10, "BBSC_IMAGE_ID")
objInsCmd.Parameters.Add( _
"BBSC_RECEIVED_DATE", OleDbType.VarChar, 10, "BBSC_RECEIVED_DATE")
objInsCmd.Parameters.Add( _
"BBSC_SEND_DATE", OleDbType.VarChar, 10, "BBSC_SEND_DATE")
objDA.InsertCommand = objInsCmd
Dim Row As DataRow = objDS.Tables(0).NewRow
Row("BBSC_TRANSACTION_ID") = "12345"
Row("BBSC_IMAGE_ID") = "1"
Row("BBSC_RECEIVED_DATE") = "01/01/2001"
Row("BBSC_SEND_DATE") = "02/02/2002"
objDS.Tables(0).Rows.Add(Row)
objDA.Update(objDS)
It fails on the objDA.Update() line... any ideas?
existing file that has a single worksheet with one row (the column
headers). Everything seems to be working except when I try to save the
file I get the above mentioned error. I'm running on Windows 2003
Server. I'm able to write a text file to the same folder I'm writing
the Excel file... and the permissions on the XLS file are read/write.
Here's my code:
Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=ExportFile.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim oConn As New OleDbConnection(sConn)
System.IO.File.CreateText("blah.txt")
'Get the list of tables (worksheets) from the Excel file
oConn.Open()
Dim schemaTable As DataTable =
oConn.GetOleDbSchemaTable(Data.OleDb.OleDbSchemaGuid.Tables, New
Object() {Nothing, Nothing, Nothing, "TABLE"})
Dim sTable As String
'There should only be one table (worksheet) in the file
'If schemaTable.Rows.Count = 1 Then
sTable = schemaTable.Rows(0)!TABLE_NAME.ToString
Dim objCmd As New OleDbCommand("SELECT * FROM [BBSC - SEND$]", oConn)
Dim objDA As New System.Data.OleDb.OleDbDataAdapter()
objDA.SelectCommand = objCmd
' Fill DataSet
Dim objDS As New DataSet()
objDA.Fill(objDS)
Dim objInsCmd As OleDbCommand = New OleDbCommand( _
"INSERT INTO [BBSC - SEND$] (BBSC_TRANSACTION_ID,
BBSC_IMAGE_ID, BBSC_RECEIVED_DATE, BBSC_SEND_DATE) " & _
"VALUES (?, ?, ?, ?)", oConn)
objInsCmd.Parameters.Add( _
"BBSC_TRANSACTION_ID", OleDbType.VarChar, 5, "BBSC_TRANSACTION_ID")
objInsCmd.Parameters.Add( _
"BBSC_IMAGE_ID", OleDbType.VarChar, 10, "BBSC_IMAGE_ID")
objInsCmd.Parameters.Add( _
"BBSC_RECEIVED_DATE", OleDbType.VarChar, 10, "BBSC_RECEIVED_DATE")
objInsCmd.Parameters.Add( _
"BBSC_SEND_DATE", OleDbType.VarChar, 10, "BBSC_SEND_DATE")
objDA.InsertCommand = objInsCmd
Dim Row As DataRow = objDS.Tables(0).NewRow
Row("BBSC_TRANSACTION_ID") = "12345"
Row("BBSC_IMAGE_ID") = "1"
Row("BBSC_RECEIVED_DATE") = "01/01/2001"
Row("BBSC_SEND_DATE") = "02/02/2002"
objDS.Tables(0).Rows.Add(Row)
objDA.Update(objDS)
It fails on the objDA.Update() line... any ideas?