Detailed description of the workaround for C # that the row already belongs to another table
The code that generated the error:
DataTable dtcontract_src = oper.getdatatable ("select * from T_contract where projectid=" + Projectid_src + "and flag=0", CON_SRC); foreach (DataRow Dr in Dtcontract_src. Rows) {String contractid_src = dr["Contractid"]. ToString (); DataTable DTCONTRACT_DST = oper.getdatatable ("select * from T_contract where projectid=" + Projectid_src + " and contractid= "+ Contractid_src +" and flag=0 ", CON_DST); if (DTCONTRACT_DST. Rows.Count! = 0) {impcontract_exist++; Continue } dtcontract_dst. Rows.Add (DR); String columns = ""; String PARAMSS = ""; OleDbCommand Updatecmd = Con_dst. CreateCommand (); foreach (DataColumn dc in DTCONTRACT_DST. Columns) {if (Columns = = "") {columns = DC. ColumnName; PARAMSS = "@" + DC. ColumnName; } else {Colu MNS + = "," + DC. ColumnName; Paramss + = "@" + DC. ColumnName; } updateCmd.Parameters.Add (New OleDbParameter (DC. ColumnName, DC. DataType); } Updatecmd.commandtext = "INSERT INTO t_contract (" + Columns + ") Values (" + Paramss + ")" ; Updatecmd.executenonquery (); OleDbDataAdapter dataadap = new OleDbDataAdapter (); Dataadap.insertcommand = Updatecmd; Dataadap.update (DTCONTRACT_DST); }
Error Line:
Dtcontract_dst. Rows.Add (DR);
Workaround:
Dtcontract_dst. Rows.Add (Dr. ItemArray);