System.InvalidOperationException: The given ColumnMapping does not match up with any column in the source or destination

Julio Bello 221 Reputation points
2021-05-21T00:27:26.033+00:00

Hi, Everybody!...

I am encountering the following error:

System.InvalidOperationException: The given ColumnMapping does not match up with any column in the source or destination.
at System.Data.SqlClient.SqlBulkCopy.AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet internalResults)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource1 source) at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)...

The exception is thrown upon executing sqlBulkCopy.WriteToServer(dataTable). Please see the following code:

    using (var sqlConnection = new SqlConnection(connectionString))  
    {  
        sqlConnection.Open();  
        using (var sqlBulkCopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))  
        {  
            sqlBulkCopy.DestinationTableName = dataTable.TableName;  

            foreach (DataColumn column in dataTable.Columns)  
                sqlBulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);  

            //var i = 0;  

            //foreach (DataColumn column in dataTable.Columns)  
            //{  
            //    SqlBulkCopyColumnMapping columnMapping = sqlBulkCopy.ColumnMappings[i++];  
            //    Console.WriteLine("{0}\t{1}\t{2}", column.ColumnName, column.ColumnName == columnMapping.SourceColumn, column.ColumnName == columnMapping.DestinationColumn);  
            //}  

            sqlBulkCopy.WriteToServer(dataTable);  
        }  
    }  

The commented debug code produces the following output:

98360-image.png

I have spent HOURS on this. What am I missing? What am I doing wrong?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,372 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,119 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-05-21T08:48:00.833+00:00

    The reason for the error is that some column names are inconsistent even though all the columns are displayed as true.

    The following piece of code causes this error:

     foreach (DataColumn column in dataTable.Columns)  
              sqlBulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);  
    

    The data of sqlBulkCopy.ColumnMappings comes from Datatable, and then you use it to compare with the column name in Datatable, it will always be equal.

    I think you should compare the column names of the Datatable with the column names of the tables in the database.

    I suggest you use the following code to check it:

                        using (SqlCommand command = new SqlCommand("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'scoretable'", sqlConnection))  
                        {  
                            DataTable columnNames = new DataTable();  
                            columnNames.Load(command.ExecuteReader());  
      
                            int i = 0;  
                            foreach (DataColumn column in dataTable.Columns)  
                            {  
                                string columnNameInDB = columnNames.Rows[i++]["COLUMN_NAME"].ToString();  
                                Console.WriteLine("{0}\t{1}\t{2}", column.ColumnName, columnNameInDB, columnNameInDB == column.ColumnName);  
                            }  
                        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful