I'm getting an exception using the SqlBulkCopy method to write a DataTable to a database table. Is there a way to get more specific information e.g. the column/value in error that is at fault because the error, as below, is not telling me much?
Input string was not in a correct format.
Failed to convert parameter value from a String to a Int32.
The given value of type String from the data source cannot be converted to type int of the specified target column.
I have checked and double checked and every data type in my datatable is the same as my database table and trying a test sample all is well. Just when I run it on a large scale it goes wrong. All entries are populated with valid data when I view the DataTable though.
My code is quite simple:
Dim trans As SqlTransaction = conn.BeginTransaction()
Try
Dim c As SqlBulkCopy = New SqlBulkCopy(conn, SqlBulkCopyOptions.Default, trans) SqlBulkCopyOptions.TableLock, trans)
c.DestinationTableName = $"CableLink"
c.WriteToServer(t)
trans.Commit()
Catch ex As Exception
trans.Rollback()
MsgBox(ex.Message)
End Try
My data table 't' is created from a List of a specific Class using reflection and this is where I checked every column type matches my class. The only real difference is some table columns are Integer mapped to smallint and Single mapped to float, but those aren't strings and the numbers fit the range.
The full stack trace is as follows
at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
at System.Data.SqlClient.SqlBulkCopy.ReadWriteColumnValueAsync(Int32 col)
at System.Data.SqlClient.SqlBulkCopy.CopyColumnsAsync(Int32 col, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyRowsAsync(Int32 rowsSoFar, Int32 totalRows, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table)
at PrepareCuReFiles.CableLinkList.SaveDataToSQLServer(String server, String db, String schema) in D:\DATA\Projects\CableLinkInfo.vb:line 519