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
Emil
  • 6,411
  • 7
  • 62
  • 112
  • 2
    is the order of the field is source and target identical? – Pleun Sep 01 '12 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. – Emil Sep 03 '12 at 12:08

1 Answers1

16

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

Pleun
  • 8,856
  • 2
  • 30
  • 50