Error in ColumnMapping does not match up with any column in the source or destination in ADO.NET C#

Gani_tpt 1,221 Reputation points
2021-10-21T06:09:31.487+00:00

I have Dataset and trying to move into SQL Server table using Bulk Option. What is the problem in the loop.

The loop (top loop) will be executing first time and inserting the record into the table without any error. where as the looping (top loop) the second time, i am getting the below error.

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

Employee column Details (DataSet)

EmpNo
Salary
Department

for (int i = 1; i <= 10; i++)
  {
    con.Open();
    bulk.DestinationTableName = "tblEmployee";
    foreach (DataColumn col in DataSet.Tables[0].Columns)
      bulk.ColumnMappings.Add(col.ColumnName, col.ColumnName);

      bulk.WriteToServer(DataSet.Tables[0]);
    con.close();
  }

pls. give us the guidance where is the problem in the above code.

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.
6,991 questions
No comments
{count} votes

Accepted answer
  1. Viorel 82,746 Reputation points
    2021-10-21T07:13:22.957+00:00

    Try executing bulk.ColumnMappings.Clear( ) before con.Open( ).

    No comments

0 additional answers

Sort by: Most helpful