Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlbulkcopy - does not allow DBNull.Value.?

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
like image 513
Emil Avatar asked Aug 31 '12 16:08

Emil


1 Answers

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

like image 197
Pleun Avatar answered Oct 20 '22 01:10

Pleun