Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

ASP.NET SQLBulkCopy String Length Problem

Hello,
Trying to use SQLBulkCopy to upload a data table into SQL.  
I'm getting this error:  
String or binary data would be truncated.
                                           sqlBulkcopy.WriteToServer(dtCustom)
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?
0
jshesek
Asked:
jshesek
  • 4
  • 3
1 Solution
 
Lokesh B RDeveloperCommented:
Hi,

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.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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.
0
 
jshesekAuthor Commented:
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
nvarchar(50)

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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Vitor MontalvãoMSSQL Senior EngineerCommented:
I never worked with the SQLBulkCopy but how can you assure the table columns mapping with the the XML file?
0
 
jshesekAuthor Commented:
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"
dt.Columns.Add(dc)

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

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

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

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
 Loop

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

Open in new window

0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
For what I read about the SQLBulkCopy you need to perform column mapping. Check for the ColumnMapping method.
0
 
jshesekAuthor Commented:
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
0
 
jshesekAuthor Commented:
The column mappings were the correct answer.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now