?
Solved

sqlbulkcopy error

Posted on 2014-12-17
4
Medium Priority
?
641 Views
Last Modified: 2014-12-18
Hi,
would you please help me out with this problem, i already stuck for couple days, any help will be greatly appreciate...

i'm working on a webpage with a temp datatable (one row and 30 columns), i try use sqlbulkcopy to insert to my sql table but with the following error:
System.InvalidOperationException: The given value of type String from the data source cannot be converted to type nvarchar of the specified target column. ---> System.InvalidOperationException: String or binary data would be truncated. at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed) --- End of inner exception stack trace --- at System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed) at System.Data.SqlClient.SqlBulkCopy.ReadWriteColumnValueAsync(Int32 col) at System.Data.SqlClient.SqlBulkCopy.CopyColumnsAsync(Int32 col, TaskCompletionSource`1 source) at System.Data.SqlClient.SqlBulkCopy.CopyRowsAsync(Int32 rowsSoFar, Int32 totalRows, CancellationToken cts, TaskCompletionSource`1 source) at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source) at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source) at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source) at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source) at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken) at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken) at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState) at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table) at Embroidery_embroideryOrder.setTrailNum(DataTable dt) in C:\temp\embroideryOrder.aspx.vb:line 1438

here's my code:
                  'create datatable
                  Dim dt As DataTable = New DataTable
                  dt.Columns.Add(New DataColumn("c1", GetType(String)))
                  dt.Columns.Add(New DataColumn("c2", GetType(String)))
                  dt.Columns.Add(New DataColumn("c3", GetType(String)))
                  dt.Columns.Add(New DataColumn("c4", GetType(Integer)))
                  dt.Columns.Add(New DataColumn("c5", GetType(Integer)))
                  ...
                  dt.Columns.Add(New DataColumn("c28", GetType(String)))
                  dt.Columns.Add(New DataColumn("c30", GetType(Decimal)))
                  
                  'assign values
                  dim row = dt.NewRow()
                  row("c1") = "simon"
                  row("c2") = "crew"
                  row("c3") = "1234"
                  row("c4") = 2
                  row("c5") = 5
                  ...
                  row("28") = "VC"
                  row("c30") = 3.40
                  dt.Rows.Add(row)
                  
                  'display without issue
                  GridView2.DataSource = dt
                  GridView2.DataBind()
                  GridView2.Visible = True
                  
                  'insert to sql
                  Dim con2 As String = ConfigurationManager.ConnectionStrings("TestCon").ConnectionString
                  Dim copy As SqlBulkCopy
                  copy = New SqlBulkCopy(con2, SqlBulkCopyOptions.KeepIdentity)
                  Using copy
                        copy.BatchSize = 25
                        copy.DestinationTableName = "dbo.embTest"
                        copy.ColumnMappings.Add("c1", "c1")
                        copy.ColumnMappings.Add("c2", "c2")
                        copy.ColumnMappings.Add("c3", "c3")
                        copy.ColumnMappings.Add("c4", "c4")
                        copy.ColumnMappings.Add("c5", "c5")
                        ...
                        copy.ColumnMappings.Add("c28", "c28")
                        copy.ColumnMappings.Add("c30", "c30")
                        Try
                              copy.WriteToServer(dt)
                        Catch ex As Exception
                              Label6.Text = ex.ToString
                              Label6.Visible = True
                        Finally
                              copy.Close()
                        End Try
                  End Using
                  
here's my sql table definition:
CREATE TABLE [dbo].[embTest](
      [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
      [c1] [nvarchar](17) NULL,
      [c2] [nvarchar](20) NULL,
      [c3] [nvarchar](100) NULL,
      [c4] [int] NULL,
      [c5] [int] NULL,
      ...
      [c29] [smalldatetime] NULL,
      [c30] [decimal] (10,4) NULL
 CONSTRAINT [PK_embTest] PRIMARY KEY CLUSTERED
(
      [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

thank you.
0
Comment
Question by:IUSR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 1500 total points
ID: 40507629
Make sure the data values in the columns does not contain any quotes inside the string,
make sure the length of the input data is not more than the length of the column it is designed for.
0
 

Author Comment

by:IUSR
ID: 40507662
Hi Raheman,

i triple checked the string and column length in my table: the string is 1000 and sql table datatype is nvarchar(100)...
i have no clue what went wrong... =(
0
 

Author Closing Comment

by:IUSR
ID: 40508174
sorry, when I go through the original data in my datatable, c2 has length 60 but when I create my new datatable, I set it to 20...
now everything is working. thank you.
0
 
LVL 25

Expert Comment

by:chaau
ID: 40508182
Why is this line does not contain "c"?
row("28") = "VC"

Open in new window

Shouldn't it be:
row("c28") = "VC"

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Suggested Courses

762 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