ASP.NET SQLBulkCopy String Length Problem

Posted on 2015-02-24
Last Modified: 2015-02-25
Trying to use SQLBulkCopy to upload a data table into SQL.  
I'm getting this error:  
String or binary data would be truncated.
The SQL column is defined as  nvarchar(50)
I changed the column to nvarchar(150) and same problem
One of the actual lines that's blowing it is: Enter the Location of Inspection
This character length is 32 with spaces
If I remove "on"  then it works and imports.  

So what's the deal with a column defined as 50 nvarchar and 32 characters going into it.?

Any ideas?
Question by:jshesek
  • 4
  • 3
LVL 14

Expert Comment

by:Lokesh B R
ID: 40630103

Please check the table structure and i think you will find the length of one or more fields where the length is not big enough to hold the data which you are inserting.

Check the data is having any spaces at beginning.
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40630362
Maybe the error message is misleading you. Sometimes happens, unfortunaly.
Can you check if there is more columns in that line that it should? Verify what field separator character are you using.

Author Comment

ID: 40630811
The file being read in is XML and here' s the actual info
<hover>Enter the Location of Inspection</hover>

And just for the heck of it I did do a TRIM at one point - same problem.  
Also adjusted the database field to 150 length and no change.
Here's the column definition in SQL

Funny thing is if I alter the text to
<hover>Enter the Location of Inspecti</hover>
it goes in.   30 Chars with spaces / 26 without spaces

Is there anything with the nvarchar?
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40630865
I never worked with the SQLBulkCopy but how can you assure the table columns mapping with the the XML file?
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.


Author Comment

ID: 40630907
I read the xml file into a datatable.  Then upload the datatable to the SQL table.  
Here's some of the code:
Dim dt As DataTable = New DataTable
Dim dc As DataColumn

dc = New DataColumn()
dc.DataType = Type.GetType("System.Int32")
dc.ColumnName = "I_CUST_CSTM_FLD"

dc = New DataColumn()
dc.DataType = Type.GetType("System.String")
dc.ColumnName = "I_CUST"

dc = New DataColumn()
dc.DataType = Type.GetType("System.String")
dc.ColumnName = "C_ACT_TYP"

dc = New DataColumn()
dc.DataType = Type.GetType("System.String")
dc.ColumnName = "T_FLD_HOVER"

Dim reader As XmlTextReader = New XmlTextReader(CustomPath)
Do While reader.Read()

Select Case reader.NodeType

 Case XmlNodeType.Element
     thisElementName = reader.Name.ToString().ToLower()
     If (thisElementName = "customfields.customfield") Then
	 rowC = dtCustom.NewRow()
     End If

 Case XmlNodeType.Text
     If (thisElementName = "id") Then
	 rowC("I_CUST_CSTM_FLD") = CInt(reader.Value.ToString())
     End If

     If (thisElementName = "companyid") Then
	 rowC("I_CUST") = reader.Value.ToString()
     End If

     If (thisElementName = "activityType") Then
	 rowC("C_ACT_TYP") = reader.Value.ToString()
     End If

     If (thisElementName = "hover") Then
	 rowC("T_FLD_HOVER") = reader.Value.ToString()
     End If
   End Select

dtCustom.Rows.Add(rowC)        '  Adds last record in to table

Open in new window

LVL 46

Accepted Solution

Vitor Montalvão earned 500 total points
ID: 40630944
For what I read about the SQLBulkCopy you need to perform column mapping. Check for the ColumnMapping method.

Author Comment

ID: 40631099
WOW that was it.  You know I had the datatable columns named the same as in the SQL table.  And I read someplace where you didn't have to map when the names were the same.  

Thanks a bunch Vitor

Author Closing Comment

ID: 40631103
The column mappings were the correct answer.

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now