12

This is my code with the following columns and in the DB, those columns are nvarchars.

SqlBulkCopy bulkCopy = new SqlBulkCopy(connection, 
System.Data.SqlClient.SqlBulkCopyOptions.Default, transaction);
bulkCopy.DestinationTableName = "Test";
bulkCopy.ColumnMappings.Add("Number", "Code");
bulkCopy.ColumnMappings.Add("Type", "Type");
bulkCopy.ColumnMappings.Add("Group", "Group");
bulkCopy.ColumnMappings.Add("Short Text", "ShortText");
bulkCopy.ColumnMappings.Add("Text", "Description");
bulkCopy.WriteToServer(dataTable);

I am trying to insert a whole data table in a DB, using bulk copy but I am getting this error:

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

Erik Philips
54.9k11 gold badges133 silver badges158 bronze badges
asked Feb 14, 2012 at 7:33
2
  • You'll need to show us some table structures, code and so forth - just with that error, we cannot possibly help you ..... - see tinyurl.com/so-hints Commented Feb 14, 2012 at 7:35
  • please check you source data. There might be invalid data in the source. Commented Feb 14, 2012 at 7:43

2 Answers 2

19

It may be caused by a too-short-column. If your ShortText column is defined as NVARCHAR(30) and you have a 40 character string, you may get that error. See here for example: http://forums.asp.net/p/1228890/2212376.aspx

answered Feb 14, 2012 at 7:43
Sign up to request clarification or add additional context in comments.

2 Comments

Bingo! This was what my problem was. Good tip, and an example of the error message not telling you the 'whole story'.
Awesome! Saved my life ;)
4

I have another answer for this question, maybe it can come in handy for someone. If you have a String which contains " (quotation mark), you get this error message too. At least I just got this error message, and I searched for the wrong row, and I found this. As I removed the quotation marks, the error disappeared.

answered May 15, 2014 at 12:18

2 Comments

I spent hours trying to resolve this error and this turned out to be the culprit for me too. I just copied all of the data from my source database into an Excel spreadsheet, searched the sheet for the bad character and found one instance.
Is there a way to make it work with quotation marks, or insert them back after import somehow?

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.