I'm getting an InvalidOperationException error from SqlBulkCopy because I'm adding null data, yet the table has no null values.

asp.net c# sqlbulkcopy


I have a table where one of the fields is "InDatabase" (SQL Server "bit" type) which does not allow nulls.

I create a table and add a few hundred rows to it, where InDatabase is always assigned a value (InDatabase refers to whether the row is in another database)

When I call SqlBulkCopy, it is giving me an InvalidOperationException Error, with the message of:

"Column 'InDatabase' does not allow DBNull.Value."

Each Row Is Created as such:

ProductionDatabaseDataSet.EntriesV2Row NewRow = this.InsertTable.NewEntriesV2Row();

NewRow.MeetEntryID = MeetEntryID;
NewRow.EventID = EventID;
NewRow.MeetID = MeetID;
NewRow.AthleteID = AthleteID;
NewRow.Exhibition = Exhibition;
NewRow.Bonus = Bonus;
NewRow.EnterEvent = true;
NewRow.InDatabase = true;
if (AutoTime != null)
    NewRow.AutoTime = AutoTime ?? -1;
if (CustomTime != null)
    NewRow.CustomTime = CustomTime ?? -1;


Then, after the above is called around 300 times, the SqlBulkCopy is called:

SqlBulkCopy bulkCopy = new SqlBulkCopy(this.Connection.ConnectionString);
bulkCopy.DestinationTableName = this.Adapter.TableMappings[0].DataSetTable;
            bulkCopy.BatchSize = BatchSize;
bulkCopy.WriteToServer(InsertTable); //Throwing the error

I use this exact same format on about a dozen other tables with no problem.

10/22/2011 8:03:24 AM

Accepted Answer

OK, I got it to work, but not really figured it out. I changed the bulkCopy section to this:

bulkCopy.DestinationTableName = this.Adapter.TableMappings[0].DataSetTable;
for (int i = 0; i < this.Adapter.TableMappings[0].ColumnMappings.Count; i++)
    bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(
bulkCopy.BatchSize = BatchSize;


Not real sure why the dozen other times I did this without setting the table mappings like this worked, but for some reason, with this table, it will not work unless I do the above.

10/22/2011 8:02:47 AM

Popular Answer

If I understood your problem correctly...

I think the problem is defining NULL value in c# .net and SQL. The mean of NULL of your programming language will not be the same in SQL. In SQL the NULL value represnt by System.DBNull Class

Just check How to Handle Null and DBNull

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow