An item with the same key has already been added

An item with the same key has already been added

montoyammontoyam Posts: 441Questions: 106Answers: 5
edited October 2020 in DataTables

I have a MVC .net framework project and on one of the DataTables I am getting the error: An item with the same key has already been added

I have even cleared out the data in the tables that are in use by that DataTable.

I am not able to post a link to the site. What can I do to troubleshoot this issue?

Here is the dataTable in question:

    var CasesEditor = new $.fn.dataTable.Editor({
        ajax: 'api/Cases',
        table: '#Cases',
        fields: [
            { label: "Case Number:", name: "Cases.CaseNumber" },
            {
                label: "Date Received:",
                name: "Cases.DateReceived",
                type: "datetime",
                format: 'MM-DD-YYYY',
                def: function () {
                    var d = new Date();
                    return d;
                }
            },
            {
                label: "Process Server:",
                name: "Cases.ProcessServerID",
                type: "select",
                placeholder: "<Select Server>",
                placeholderValue: 0,
                placeholderDisabled: false
            },
            {
                label: "Trial Date:",
                name: "Cases.TrialDate",
                type: "datetime",
                format: 'MM-DD-YYYY'
            },
            {
                label: "Case Status:",
                name: "Cases.CaseStatusID",
                type: "select",
                placeholder: "<Select a Status>",
                placeholderValue: 0,
                placeholderDisabled: false
            },
            { name: "Cases.EnteredBy", label: "Added By", type: "hidden", def: staffIDCookie },
            {
                label: "Date Added:",
                name: "Cases.DateAdded",
                type: "hidden",
                def: function () {
                    var d = new Date();
                    return d;
                }
            }
        ]
    });

    var CasesTable = $('#Cases').DataTable({
        searchPanes: {
            cascadePanes: true,
            viewTotal: true,
            controls: false,
            layout: 'columns-6',
            threshold: 1,
            columns: [1,2,3,4], //alertDates, alerts, CaseStatus, openActions
            emptyMessage: "<i><b>none</b></i>"
        },
        dom: "PBfrtip",
        ajax: 'api/Cases',
        columns: [
            //first column is blank and using CSS (class details-control) an icon will be here if note records exist                
            {
                class: "details-control",
                "orderable": false,
                data: null,
                defaultContent: ''  //'<a href="#"  onclick="jumpto();">Notes</a>'
            },
            {
                title: "Important Dates",
                data: "vw_Alerts_Dates",
                visible: false,
                render: {
                    _: '[, ].Alert',
                    sp: '[].Alert'
                },
                searchPanes: {
                    orthogonal: 'sp',
                    show: true
                }
                
            },
            {
                title: "Alerts",
                data: "vw_Alerts",
                visible: false,
                
                render: {
                    _: '[, ].Alert',
                    sp: '[].Alert'
                },
                
                searchPanes: {
                    orthogonal: 'sp',
                    show: true
                }
            },
            { data: "CaseStatus.StatusName", title: "Case Status" },
            {
                title: "Open Actions",
                data: "vw_Alerts_Actions",
                visible: false,
                render: {
                    _: '[, ].Alert',
                    sp: '[].Alert'
                },
                searchPanes: {
                    orthogonal: 'sp',
                    show: true
                }

            },
            { data: "Cases.CaseNumber", title: "Case Number" },
            { data: "Cases.DateReceived", title: "Date Received" },
            {
                data: null,
                title: "Process Server",
                render: function (data, type, row) {
                    return (row.Cases.ProcessServerID == 0) ? '' : row.ProcessServers.ServerFirstName + ' ' + row.ProcessServers.ServerLastName;
                }
            },
            {
                data: "Cases.TrialDate", title: "Trial Date"
            },
            {
                data: null,
                title: "Added By",
                render: function (data, type, row) {
                    return row.EnteredByFirstName + ' ' + row.EnteredByLastName;
                }
            },
            { data: "Cases.DateAdded", title: "Date Added" }
            
            ,{
                data: "CaseDefendants",
                title: "Defendants",
                render: function (data) {
                    return data.map(function (o) {
                        return o.DefendantFirstName + ' ' + o.DefendantLastName;
                    })
                }
            }

        ],
        //for that first column, when the row is created get rid of the class if no notes exist
        createdRow: function (row, data, line, cells) {
            if (data.CaseNotes.length == 0) {
                $(cells[0]).removeClass('details-control')
            }
        },
        //*******************
        select: true,
        lengthChange: false,
        buttons: [
            { extend: "create", editor: CasesEditor },
            { extend: "edit", editor: CasesEditor },
            { extend: "remove", editor: CasesEditor }
        ]
    });
    $('#Cases tbody').on('click', 'td.details-control', function () {
        $("#notesJump")[0].scrollIntoView();
    });

with this controller:

    public class CasesController : ApiController
    {
        [Route("api/Cases")]
        [HttpGet]
        [HttpPost]
        public IHttpActionResult Cases()
        {
            var request = HttpContext.Current.Request;
            var settings = Properties.Settings.Default;

            using (var db = new Database(settings.DbType, settings.DbConnection))
            {
                var response = new Editor(db, "Cases", "CaseID")
                    .Model<CasesModel>("Cases")
                    .Field(new Field("Cases.CaseNumber")
                        .Validator(Validation.NotEmpty())
                    )
                    .Field(new Field("Cases.ClientIdentifier as ClientName").Set(false))
                    .Field(new Field("Cases.DateReceived")
                        .Validator(Validation.NotEmpty())
                        .GetFormatter(Format.DateSqlToFormat("MM/dd/yyyy"))
                    )
                    .Field(new Field("Cases.PropertyID")
                        .Validator(Validation.NotEmpty())
                        .Options(new Options()
                                    .Table("Properties")
                                    .Value("PropertyID")
                                    .Label("PropertyAddress")
                        )
                    )
                    .Field(new Field("Cases.CaseStatusID")
                        .Validator(Validation.NotEmpty())
                        .Options(new Options()
                                    .Table("CaseStatus")
                                    .Value("CaseStatusID")
                                    .Label("StatusName")
                        )
                    )
                    .Field(new Field("Cases.ProcessServerID")
                        .Options(new Options()
                                    .Table("ProcessServers")
                                    .Value("ProcessServerID")
                                    .Label(new[] { "ServerFirstName", "ServerLastName" })
                                    .Render(row => row["ServerLastName"] + ", " + row["ServerFirstName"])
                        )
                    )
                    .Field(new Field("Cases.TrialDate")
                        .SetFormatter(Format.NullEmpty())
                        .GetFormatter(Format.DateSqlToFormat("MM/dd/yyyy"))
                    )
                    .Field(new Field("Cases.DateAdded")
                        .Set(false)
                    )
                    .Field(new Field("Cases.EnteredBy")
                        .Options(new Options()
                                    .Table("Staff")
                                    .Value("StaffID")
                                    .Label(new[] { "StaffFirstName", "StaffLastName" })
                                    .Render(row => row["StaffLastName"] + ", " + row["StaffFirstName"])
                        )
                    )
                    .LeftJoin("CaseStatus", "CaseStatus.CaseStatusID", "=", "Cases.CaseStatusID")
                        .Model<CaseStatusModel>("CaseStatus")
                    .LeftJoin("Properties", "Properties.PropertyID", "=", "Cases.PropertyID")
                        .Field(new Field("Properties.PropertyAddress"))
                        .Field(new Field("Properties.PropertyName"))
                    .LeftJoin("ProcessServers", "ProcessServers.ProcessServerID", "=", "Cases.ProcessServerID")
                        .Model<ProcessServerModel>("ProcessServers")
                    .LeftJoin("CaseStatus", "CaseStatus.CaseStatusID", "=", "Cases.CaseStatusID")
                        .Model<CaseStatusModel>("CaseStatus")
                    .LeftJoin("Staff as EnteredByStaff", "EnteredByStaff.StaffID", "=", "Cases.EnteredBy")
                        .Field(new Field("EnteredByStaff.StaffFirstName as EnteredByFirstName"))
                        .Field(new Field("EnteredByStaff.StaffLastName as EnteredByLastName"))
                    .MJoin(new MJoin("CaseDefendants")
                        .Link("CaseDefendants.CaseID", "Cases.CaseID")
                        .Model<CaseDefendantsModel>()
                    )
                    /*
                    .MJoin(new MJoin("CaseActions")
                        .Link("CaseActions.CaseID", "Cases.CaseID")
                        .Model<CaseActionsModel>()
                        .Field(new Field("FollowUpDate")
                            .GetFormatter(Format.DateSqlToFormat("MM/dd/yyyy"))
                        )
                        .Where(q =>
                            q.Where("FollowUpDate", null, "!=")
                            .Where("CompletedDate", null, "=")
                            )
                    )
                    */
                    .MJoin(new MJoin("vw_Alerts")
                        .Link("vw_Alerts.CaseID", "Cases.CaseID")
                        .Field(new Field("CaseNumber"))
                        .Field(new Field("Alert"))
                    )
                    .MJoin(new MJoin("vw_Alerts_Dates")
                        .Link("vw_Alerts_Dates.CaseID", "Cases.CaseID")
                        .Field(new Field("CaseNumber"))
                        .Field(new Field("Alert"))
                    )
                    .MJoin(new MJoin("vw_Alerts_Actions")
                        .Link("vw_Alerts_Actions.CaseID", "Cases.CaseID")
                        .Field(new Field("CaseNumber"))
                        .Field(new Field("Alert"))
                    )
                    .MJoin(new MJoin("CaseNotes")
                        .Link("CaseNotes.CaseID", "Cases.CaseID")
                        .Field(new Field("CaseID"))
                    )
                    .Process(request)
                    .Data();
                return Json(response);
            }
        }
    }

Answers

  • montoyammontoyam Posts: 441Questions: 106Answers: 5

    oh my goodness. I just saw the issue in my controller. Lines 62-63 are the same as 69-70. Just like the error said...duplicate key.

    sorry, please close this question.

  • LapointeLapointe Posts: 373Questions: 70Answers: 4

    Hi...

    It is not possible to close our proper question when the question is no more usefull...

This discussion has been closed.