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

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

dynasoftdynasoft Posts: 446Questions: 69Answers: 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: 63,523Questions: 1Answers: 10,473 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: 446Questions: 69Answers: 3

    Will try that. Thanks

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 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: 446Questions: 69Answers: 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: 446Questions: 69Answers: 3

    Disabling all AntiForgeryToken does nothing

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

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

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Kindly advise

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Please let us know. Thanks.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Apologies for the delayed reply here. I think it is this line where it is failing, but there is a check to ensure it doesn't fail.

    What version of the .NET libraries for Editor are you using? 2.3.2 is the current release.

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    1.9.6

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    We use the same code for 2 other similar types of data and do not get the error strangely. Only the fields in VoiceMYTABLEData (called DataMYTABLEData and ServiceMYTABLEData in the other classes) change.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I've double checked all fields in the js editor code and tthey are all unique

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Same for the server code. All fields r unique.
    If i use this instead of the mjoin, it works:
    editor.Field(new Field("VoiceMYTABLEInvoice.InvoiceNumber")
    .Set(false)
    );

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Oh gosh, that's an old version! The error is this line in 1.9.6.

    That line is code is still present in the 2.3.2 current release, but there have been changes around it. What I would suggest is trying the 2.3.2 dll - it will work fine with the 1.9.6 client-side.

    If that doesn't resolve it outright (there is a fair chance it won't - but with such a large version difference and 1.x no longer getting updates, it is worth trying), then it will be something to do with the data that I haven't anticipated. I would need a copy of your data and db schema, plus the controller code and if you are using models, them as well.

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    1.9.6 works for everything else fine. Updating any web component can cause major issues elsewhere (css...), hence why we never updated DT.
    It would still be nice to know whick keys its loading twice. Is there a way?

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3
    edited May 21

    Could it be this: I have 2 fields in VoiceMyTABLEData, one called LinkedVoicePlan and LinkedVoicePlanUsage. Could it be that your c# Dictionary is hitting the 1st field and because it contains the same letters as in LinkedVoicePlanUsage it thinks it's already loaded as a field name?

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I think I see what it could be. Will revert back shortly.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I got it to work. Thanks. I had another field from VoiceMYTABLEData that needed select elements in the DT, like InvoiceNumber.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Excellent - delighted to hear you have a solution! Thanks for letting me know.

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Thanks again allan. I will upgrade but only once things quieten down with dev work.

Sign In or Register to comment.