Error "An item with the same key has already been added."

Error "An item with the same key has already been added."

dynasoftdynasoft Posts: 429Questions: 68Answers: 3
edited May 14 in DataTables 1.9

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem: I'm working with MVC c# and Editor Datatables from http://editor.datatables.net. i'm getting the error below on line 'editor.Process(formData);' of my server code:

An item with the same key has already been added.

What could be the cause and how can i debug the error to identify what keys are being added?

Here's my code:

  • A class:
public class VoiceMYTABLEDataDBModel
{
    public class VoiceMYTABLEData
    {
        public long id { get; set; }
        public string Customer_ID { get; set; }
    }

    public class VoiceMYTABLEInvoice
    {
        public long id { get; set; }
        public long CustomerID { get; set; }
        public long MYTABLEDataID { get; set; }
        public string InvoiceNumber { get; set; }
    }
}
  • Controller code:
        public DtResponse CRUDRatedVoiceMYTABLEData(string strFromDate, string strToDate, int intExportToFile, MYTABLEDataUISettings lblo)
        {
            Editor editor = null;
            NameValueViewModel[] arrNVVM = null;
            Task t;

            try
            {
                HttpRequest formData = HttpContext.Current.Request;

                CustomersModel CM = new CustomersModel();
                arrNVVM = CM.GetAllCustomerIDsNbrs_arr();
                CM = null;

                if (arrNVVM == null) arrNVVM = new NameValueViewModel[] { };

                using (Database db = new Database(SetGetDbType2, SetGetDbConnection))
                {
                    editor = new Editor(db, "VoiceMYTABLEData", "VoiceMYTABLEData.id").Model<VoiceMYTABLEDataDBModel.VoiceMYTABLEData>("VoiceMYTABLEData");
                    editor.Field(new Field("VoiceMYTABLEData.id")
                        .Set(false)
                    );
                    editor.Field(new Field("VoiceMYTABLEData.Customer_ID")
                        .Set(false)
                    );
                    editor.LeftJoin("VoiceMYTABLEInvoice", "VoiceMYTABLEData.id", "=", "VoiceMYTABLEInvoice.MYTABLEDataID")
                        .MJoin(new MJoin("VoiceMYTABLEInvoice")
                        .Model<VoiceMYTABLEDataDBModel.VoiceMYTABLEInvoice>()
                        .Name("VoiceMYTABLEInvoice")
                        .Link("VoiceMYTABLEData.id", "VoiceMYTABLEInvoice.MYTABLEDataID")
                        .Order("VoiceMYTABLEInvoice.InvoiceNumber ASC")
                        .Field(new Field("id")
                            .Options(new Options()
                                .Table("VoiceMYTABLEInvoice")
                                .Value("id")
                                .Label("InvoiceNumber")
                                .Order("InvoiceNumber ASC")
                                .Render(row =>
                                {
                                    return dicPCEAValues["InvoiceNumber"].ToString();
                                }).Order("InvoiceNumber ASC")
                            )
                            .Set(false)
                        )
                        .Set(false)
                    );

                    editor.Where("VoiceMYTABLEData.Date_Time_Called", CommonUtilities.ToDate2(strFromDate + " 00:00:01"), ">=");
                    editor.Where("VoiceMYTABLEData.Date_Time_Called", CommonUtilities.ToDate2(strToDate + " 23:59:59"), "<=");

                    editor.TryCatch(false);
                    editor.Debug(true);
                    editor.Process(formData);
                }
                arrNVVM = null;
            }
            catch (Exception ex)
            {
                
            }
            return editor.Data();
        }
  • client js code:
function CRUDRatedVoiceMYTABLEData(strFromDate1, strToDate1, intExportToFile1) {

    var strSelOpt = '';
    var strSelSta = '<select class="form-control">';
    var strSelEnd = '</select>';

    var token = $('input[name="__RequestVerificationToken"]').val();

    dataTable2 = $('#tblRatedVoiceMYTABLEDataTable').DataTable({

        destroy: true,
        responsive: true,
        processing: true,
        deferRender: true,
        select: true,
        order: [[0, 'desc']],
        pageLength: 10,
        columnDefs: [
            { 'bVisible': false, 'targets': 0 },
            {
                'targets': [0, 1, 2],
                className: 'text-center'
            }
        ],
        dom: 'Bfrtip',
        ajax: {
            url: '/users/' + strAccountIdx1 + '/Admin/MYTABLEData/CRUDRatedVoiceMYTABLEData/',
            data: function (d) {
                return $.extend({}, d, {
                    strFromDate: strFromDate1,
                    strToDate: strToDate1,
                    intExportToFile: intExportToFile1,
                    __RequestVerificationToken: token
                });
            },
            //type: 'GET',
            type: 'POST',
            dataType: 'json',
            //contentType: 'application/json; charset=utf-8',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            async: true,
            cache: false
        },
        columns: [
            {
                data: 'VoiceMYTABLEData.id',
                className: 'text-left'
            },
            {
                data: 'VoiceMYTABLEData.Customer_ID',
                className: 'text-left'
            },
            {
                data: 'VoiceMYTABLEInvoice',
                className: 'text-left'
            }
        ],
        buttons: [
            { extend: 'edit', editor: editor3, formButtons: [] }
        ]
    });
}
  • create script for the db tables:
CREATE TABLE IF NOT EXISTS `[DBNAME]`.`VoiceMYTABLEData` (
  `ID` BIGINT NOT NULL AUTO_INCREMENT,
  `Customer_ID` BIGINT DEFAULT '0',
  PRIMARY KEY (`ID`),
  UNIQUE INDEX `Index_PrimaryKey` (`ID`),
  INDEX `Index_Customer_ID` (`Customer_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `[DBNAME]`.`VoiceMYTABLEInvoice`;;
CREATE TABLE IF NOT EXISTS `[DBNAME]`.`VoiceMYTABLEInvoice` (
  `ID` BIGINT NOT NULL AUTO_INCREMENT,
  `CustomerID` BIGINT DEFAULT '0',
  `MYTABLEDataID` BIGINT DEFAULT '0',
  `InvoiceNumber` VARCHAR(55) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE INDEX `Index_PrimaryKey` (`ID`),
  INDEX `Index_CustomerID` (`CustomerID`),
  INDEX `Index_MYTABLEDataID` (`MYTABLEDataID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

Answers

  • allanallan Posts: 61,970Questions: 1Answers: 10,160 Site admin

    If you remove your try / catch, do you get a back trace indicating where in the library the error is happening? That might give me a clue as to what is going on.

    Thanks,
    Allan

  • dynasoftdynasoft Posts: 429Questions: 68Answers: 3

    Will try that. Thanks

  • dynasoftdynasoft Posts: 429Questions: 68Answers: 3

    System.ArgumentException
    HResult=0x80070057
    Message=An item with the same key has already been added.
    Source=mscorlib
    StackTrace:
    at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
    at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value)
    at DataTables.MJoin.Data(Editor editor, DtResponse response) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/MJoin.cs:line 466
    at DataTables.Editor._Get(Object id, DtRequest http) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1473
    at DataTables.Editor._Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1216
    at DataTables.Editor.Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 887
    at DataTables.Editor.Process(NameValueCollection data, String culture) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 943
    at DataTables.Editor.Process(HttpRequest request, String culture) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 969
    at YYY.Model.MYTABLEDataModel.CRUDRatedVoiceMYTABLEData(String strFromDate, String strToDate, Int32 intExportToFile, MYTABLEDataUISettings lblo) in Z:\Dev\Projects\XXX\Active\YYY.Model\Model\MYTABLEDataModel.cs:line 542
    at YYY.Web.Controllers.MYTABLEDataController.CRUDRatedVoiceMYTABLEData(String strFromDate, String strToDate, Int32 intExportToFile) in Z:\Dev\Projects\XXX\Active\YYY.Web\Controllers\Admin\MYTABLEDataController.cs:line 341
    at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters)
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

    This exception was originally thrown at this call stack:
    [External Code]
    DataTables.MJoin.Data(DataTables.Editor, DataTables.DtResponse) in MJoin.cs
    DataTables.Editor._Get(object, DataTables.DtRequest) in Editor.cs
    DataTables.Editor._Process(DataTables.DtRequest) in Editor.cs
    DataTables.Editor.Process(DataTables.DtRequest) in Editor.cs
    DataTables.Editor.Process(System.Collections.Specialized.NameValueCollection, string) in Editor.cs
    DataTables.Editor.Process(System.Web.HttpRequest, string) in Editor.cs
    YYY.Model.MYTABLEDataModel.CRUDRatedVoiceMYTABLEData(string, string, int, YYYCDLL.ViewModel.MYTABLEDataUISettings) in MYTABLEDataModel.cs
    YYY.Web.Controllers.MYTABLEDataController.CRUDRatedVoiceMYTABLEData(string, string, int) in MYTABLEDataController.cs
    [External Code]
    ...
    [Call Stack Truncated]

  • dynasoftdynasoft Posts: 429Questions: 68Answers: 3

    Stack trace from Application_EndRequest:

    " at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)\r\n at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)\r\n at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value)\r\n at DataTables.MJoin.Data(Editor editor, DtResponse response) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/MJoin.cs:line 466\r\n at DataTables.Editor._Get(Object id, DtRequest http) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1473\r\n at DataTables.Editor._Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1216\r\n at DataTables.Editor.Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 887\r\n at DataTables.Editor.Process(NameValueCollection data, String culture) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 943\r\n at DataTables.Editor.Process(HttpRequest request, String culture) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 969\r\n at YYY.Model.MyTableDataModel.CRUDRatedVoiceMyTableData(String strFromDate, String strToDate, Int32 intExportToFile, MyTableDataUISettings lblo) in Z:\Dev\Projects\XXX\Active\YYY.Model\Model\MyTableDataModel.cs:line 542\r\n at YYY.Web.Controllers.MyTableDataController.CRUDRatedVoiceMyTableData(String strFromDate, String strToDate, Int32 intExportToFile) in Z:\Dev\Projects\XXX\Active\YYY.Web\Controllers\Admin\MyTableDataController.cs:line 341\r\n at lambda_method(Closure , ControllerBase , Object[] )\r\n at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)\r\n at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters)\r\n at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters)\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_6.<BeginInvokeAction>b__4()\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()\r\n at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Controller.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()\r\n at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Controller.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()\r\n at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)\r\n at System.Web.Mvc.MvcHandler.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()\r\n at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)\r\n at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)\r\n at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()\r\n at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)\r\n at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)"

  • dynasoftdynasoft Posts: 429Questions: 68Answers: 3

    Disabling all AntiForgeryToken does nothing

  • dynasoftdynasoft Posts: 429Questions: 68Answers: 3

    Hi, how can we check what keys are already added by DT? We're really stuck at the moment. Thanks.

  • dynasoftdynasoft Posts: 429Questions: 68Answers: 3

    Kindly advise

Sign In or Register to comment.