SqlBulkCopy dbnull.value error

c# excel guid sqlbulkcopy

Question

I am trying to insert rows from an excel file into sql server 2000 using bulkcopy. In the table there is a 'rowguid' field and its default value is set to (newid()) and cannot except null values. Also RowGUID is set to "Yes".

In my code i remove the column mapping for rowguid . Here is my code.

if (dr.HasRows)
{
    using (SqlBulkCopy bulkCopy =
        new SqlBulkCopy(sqlConnectionString))
        {
           bulkCopy.DestinationTableName = "configtest";
           SqlBulkCopyColumnMapping value = new SqlBulkCopyColumnMapping("rowguid", "rowguid");
           bulkCopy.ColumnMappings.Remove(value);
           bulkCopy.WriteToServer(dr);
         }


 }

I get column 'rowguid does not allow dbnull.value

definition for that field is : rowguid , uniqueidentifier, allow nulls unticked.

Popular Answer

You have to remove the mapping that is referenced. So if you already have ColumnMappings you have to do it like this:

var mapping = bulkCopy.ColumnMappings.Cast<SqlBulkCopyColumnMapping>()
            .Single(x => x.DestinationColumn == "rowguid"));

bulkCopy.ColumnMappings.Remove(mapping);



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why