0

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?

marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Jul 29, 2014 at 20:36
13
  • How you are creating the datatable? define table column exact as target table. Commented Jul 29, 2014 at 20:39
  • @Rahul it is a long story, I am using CsvHelper library to read data from about 500 csv file and load that data into a datatable. Anyway, that works fine and I don't think that the datatable has any mistake Commented Jul 29, 2014 at 20:40
  • @Rahul yes sure, the table columns are exactly as the datatable columns. 100% sure of that. I am aware to this point Commented Jul 29, 2014 at 20:41
  • OK. In that case, 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. Can you check and confirm that. Commented Jul 29, 2014 at 20:54
  • @Rahul I don't know if I understood you correctly, but the database table has 39 column and my csv files has 38. However the remaining 1 column is nullable . is that what you mean? Commented Jul 29, 2014 at 20:56

1 Answer 1

2

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.

answered Jul 30, 2014 at 15:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.