LittlBUGer
Programmer
Hello all. I'm having a very strange issue when using ASP.NET 2.0 (and ADO.NET 2.0) along with the SqlBulkCopy command. Basically all I'm doing is having a user upload a pre-formatted Excel file with data in which I'm using the SqlBulkCopy command to copy the data from the file into a temporary SQL Server 2000 table. This seems to work fine except for a few columns and I can't figure out why. The columns with issues thus far are dates, which is strange as there's other columns with dates that work just fine in the exact same file. Has anyone else seen such behavior like this? I'm using basic code and I just can't figure it out. Here's some parts of my code:
And here's some pieces of info from my temp table, the columns within anyway:
It's driving me mad as I just can't find any fault or why other date columns work but some don't (such as there's a 'Brand Date' and 'Wean Date' column that works just fine but the 'Birth Date' column does not). I've tried formatting the Excel file in different ways too but no luck. Any suggestions anyone? I'd be happy to post more information if needed. Thanks.
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
Code:
Protected Sub ExcelImport(ByVal thefile As String)
Dim sSQLTable As String = "TempExcelImport"
Dim sExcelFileName As String = thefile
Dim sWorkbook As String = "[VB_Stock_Data$]"
Dim sExcelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~/Temp/" & sExcelFileName) & ";Extended Properties=""Excel 8.0;HDR=YES;"""
Dim sSqlConnectionString As String = dataconnection.ConnectionString
Dim sClearSQL As String = "DELETE FROM " & sSQLTable
Dim SqlConn As SqlConnection = New SqlConnection(sSqlConnectionString)
Dim SqlCmd As SqlCommand = New SqlCommand(sClearSQL, SqlConn)
Try
SqlConn.Open()
SqlCmd.ExecuteNonQuery()
Catch ex As Exception
lblMessage.Visible = True
lblMessage.ForeColor = System.Drawing.Color.Red
lblMessage.Text = "ERROR: Excel Import Temp Table Clear Failed! - " & ex.Message
Finally
SqlConn.Close()
End Try
Dim OleDbConn As OleDbConnection = New OleDbConnection(sExcelConnectionString)
Dim OleDbStr As String = "SELECT * FROM " & sWorkbook & " WHERE [Line Num] > 5"
Dim OleDbCmd As OleDbCommand = New OleDbCommand((OleDbStr), OleDbConn)
Try
OleDbConn.Open()
OleDbCmd.ExecuteNonQuery()
Dim dr As OleDbDataReader = OleDbCmd.ExecuteReader()
Dim bulkCopy As SqlBulkCopy = New SqlBulkCopy(sSqlConnectionString)
bulkCopy.DestinationTableName = sSQLTable
bulkCopy.WriteToServer(dr)
Catch ex2 As Exception
lblMessage.Visible = True
lblMessage.ForeColor = System.Drawing.Color.Red
lblMessage.Text = "ERROR: Excel Import to Temp Failed! - " & ex2.Message
Finally
OleDbConn.Close()
End Try
End Sub
And here's some pieces of info from my temp table, the columns within anyway:
[Line Num]
Gender
[Birth Date]
[Birth Weight]
[Brand Date]
[Wean Date]
...
It's driving me mad as I just can't find any fault or why other date columns work but some don't (such as there's a 'Brand Date' and 'Wean Date' column that works just fine but the 'Birth Date' column does not). I've tried formatting the Excel file in different ways too but no luck. Any suggestions anyone? I'd be happy to post more information if needed. Thanks.
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein