9

I am trying these 2 calls below but both of them returns me an exception Customer_ID does not allow DBNull.Value. but when I debug, I see that all my records have Customer_ID assigned. It is the only one as not nullable defined on my DB table. what is causing that error?

   bulkCopy.WriteToServer(myBookingDataTable)

   bulkCopy.WriteToServer(myBookingss.ToArray())

here is my entire code.

    Using myConnection As SqlConnection = _
                New SqlConnection(connectionString)
                myConnection.Open()


                Using bulkCopy As SqlBulkCopy = _
                  New SqlBulkCopy(My.Settings.ConnectionString(), SqlBulkCopyOptions.Default)

                    bulkCopy.DestinationTableName = "dbo.Booking"

                    Try
                        ' Write from the source to the destination.
                         bulkCopy.WriteToServer(myBookingDataTable)

                        'bulkCopy.WriteToServer(myBookingss.ToArray())

                    Catch ex As Exception
                        Console.WriteLine(ex.Message)

                    Finally

                        bulkCopy.Close()
                    End Try
                End Using
2
  • 2
    is the order of the field is source and target identical? Commented Sep 1, 2012 at 19:16
  • @Pleun: thank you very much for your help. It was the problem indeed. I am using dataset.xsd item to define datatable and although i added a fresh one, it was somehow not ordered as it supposed to be. But it is kind of strange why sqlbulkcopy is that much sensitive. can you make answer also that I accept your comment as correct answer. thanks again. Commented Sep 3, 2012 at 12:08

1 Answer 1

16

Make sure the order of the fields in source and target are identical.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.