SqlBulkCopy Value cannot be null

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Problem

You execute the method WriteToServer, and the following error is thrown:

Value cannot be null.

// Oops! The destination name is null
string destinationName = null;

using(var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
    connection.Open();
    using (var sqlBulk = new SqlBulkCopy(connection))
    {
        sqlBulk.DestinationTableName = destinationName;
        sqlBulk.WriteToServer(dt);
    }
}

Try it

Solution

Cause

  • You provided a null value to the DestinationTableName property.

Fix

  • ENSURE the value you provided is not null.
string destinationName = "Customers";

using(var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
    connection.Open();
    using (var sqlBulk = new SqlBulkCopy(connection))
    {
        sqlBulk.DestinationTableName = destinationName;
        sqlBulk.WriteToServer(dt);
    }
}

Try it



Got any SqlBulkCopy Question?