0

I have below data table :-

enter image description here

I have below SQL table as Product:

enter image description here

ITEM CODE from data table is ProductCode in SQL.

CLOSING (Last column in image) from data table is Quantity in SQL.

I wanted to update ITEM CODE against CLOSING (Quantity) in SQL.

I wrote this bulk update function:

using (SqlBulkCopy bcSQL = new SqlBulkCopy(con))
{
    bcSQL.DestinationTableName = "Product";

    // Map Source column name to Destination Column name
    // Src Column is in your DataTable
    // Dest Column is column name available in your SQL Table
    bcSQL.ColumnMappings.Add(" ITEM CODE", "ProductCode");
    bcSQL.ColumnMappings.Add(" Closing", "Quantity");

    bcSQL.WriteToServer(dt);
}

But I get this error:

The given ColumnName ' ITEM CODE' does not match up with any column in data source.

Please help - how can I resolve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
C Sharper
  • 8,284
  • 26
  • 88
  • 151
  • Wild guessing because I am not working with SqlBulkCopy but try not using spaces ' ' when naming columns. I know in past it was making problems to me. Use `_` instead and try code again. Again this is wild guess but until someone else come you have something to try. – Aleksa Ristic Mar 10 '18 at 13:12
  • Can you check the column names in the dt and see if they are the same as you see them in the first image? – Chetan Mar 10 '18 at 13:16
  • Even if the message is about column names, the problem might be in the data. Check the data tpes and lengths match. Check also the default values when data is NULL. Finally, check that you don't have an identity key in those columns. Take a look at [this answer](https://stackoverflow.com/a/46561241/1182515). – Diana Mar 10 '18 at 13:32
  • Also, unrelated, but probably you should use uppercase for the `" CLOSING"` mapping. – Diana Mar 10 '18 at 13:32
  • Thanks Both Of You spacing was the issue – C Sharper Mar 10 '18 at 13:33

1 Answers1

0

Since I was filling data table from Excel , I didnt noticed spaces in column names.

There were 2 blank spaces after column names

When I modified it like :-

bcSQL.ColumnMappings.Add("ITEM CODE  ", "ProductCode"); //2 spaces

It got resolved.

Key Issue :- Column names as mentioned in comment section

C Sharper
  • 8,284
  • 26
  • 88
  • 151