can't add datatable to sqlbulkcopy

c# datatable sqlbulkcopy sql-server sql-server-2008

Question

This is my code

public static void InsertToDatabase(DataTable dataTable) {
    using (SqlBulkCopy sbc = new SqlBulkCopy(GetConnectionString())){
        sbc.DestinationTableName = tableName;
        sbc.WriteToServer(dataTable);
    }
}

This is the exception:

The given value of type String from the data source cannot be converted to type nchar of the specified target column.

Is there any way to know on which column the exception belongs?

Accepted Answer

There must be size mismatch. I mean the particular column in exception is having a size which is not able to hold the size of data in datatable.

what I mean is if column in exception is defined @DB end as say nchar(10) and you are trying to insert a data of size > 10 chars then also you will hit this exception.

Check and make sure the same.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow