I am getting the following error itermittently on a SQLBulkCopy. I have search Google high and low, but it looks like I may be the only one to ever get it given the Google result set I received... Any idea why this is happening?
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Data.SqlClient.SqlDataReader.GetLocaleId(Int32 i)
at System.Data.SqlClient.SqlBulkCopy.AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet internalResults)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternal()
at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServer(Int32 columnCount)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(IDataReader reader)
at DEER_DAQ.modFunctions.SQLBulkCopyCode()
Public Sub SQLBulkCopyCode()
Dim tempReader As SqlDataReader
Dim BufferRowCount As UInt16
Try
If inSQLBulkCopyCode Then Exit Sub
inSQLBulkCopyCode = True
While SQLBufferConn.State <> Data.ConnectionState.Open
PostMessageToStatusTab(vbNewLine & "Lost Connection to SQLBufferConn")
If ConnectToSQL(2) Then
PostMessageToStatusTab(vbNewLine & "Restablished Connection to SQLBufferConn")
Else
PostMessageToStatusTab(vbNewLine & "Unable to Restablish Connection to SQLBufferConn")
End If
System.Threading.Thread.Sleep(500)
End While
While SQLDataConn.State <> Data.ConnectionState.Open
PostMessageToStatusTab(vbNewLine & "Lost Connection to SQLDataConn")
If ConnectToSQL(1) Then
PostMessageToStatusTab(vbNewLine & "Restablished Connection to SQLDataConn")
Else
PostMessageToStatusTab(vbNewLine & "Unable to Restablish Connection to SQLDataConn")
End If
System.Threading.Thread.Sleep(500)
End While
' Get the number of rows in the buffer table
SQLBufferReadCmd = New SqlCommand("SELECT COUNT(*) FROM " & TestCell, SQLBufferConn)
BufferRowCount = SQLBufferReadCmd.ExecuteScalar
' Get all rows from the buffer (should be 100)
SQLBufferReadCmd = New SqlCommand("SELECT * FROM " & TestCell, SQLBufferConn)
tempReader = SQLBufferReadCmd.ExecuteReader
If Not SQLBulkCopyFirstRun Then
Dim TempCount As UInt16
' Create bulk copier
SQLBulkCopier = New SqlBulkCopy(SQLDataConn)
SQLBulkCopier.DestinationTableName = TestCell
' Map column headers properly
For TempCount = 0 To tempReader.FieldCount - 1
SQLBulkCopier.ColumnMappings.Add(tempReader.GetName(TempCount), tempReader.GetName(TempCount))
'PostMessageToStatusTab(tempReader.GetName(TempCount))
Next
SQLBulkCopyFirstRun = True
End If
' Start the bulk copy
SQLBulkCopier.BatchSize = BufferRowCount
SQLBulkCopier.WriteToServer(tempReader)
' Delete all data from the buffer table
SQLBufferReadCmd = New SqlCommand("DELETE FROM " & TestCell, SQLBufferConn)
SQLBufferReadCmd.ExecuteNonQuery()
' Reset the buffer size counter
SQLBufferSize = 0
Catch ex As Exception
SQLBulkCopyError = True
PostMessageToStatusTab("Error in SQL bulk Copy!" & vbNewLine & vbNewLine & ex.ToString)
TextMessage("Error in SQL bulk Copy!" & vbNewLine & vbNewLine & ex.ToString)
Finally
inSQLBulkCopyCode = False
tempReader.Close()
End Try
End sub