0

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.

Lucky Luke2
  • 2,056
  • 5
  • 25
  • 32

2 Answers2

0

Instead of

bulkCopy.ColumnMappings.Remove(value);

Try

bulkCopy.ColumnMappings.Add(value);
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
0

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);
Hoarst
  • 171
  • 2
  • 9