Server-side searching or filtering won't work (fnServerData+LINQ+DataTableParser)

Server-side searching or filtering won't work (fnServerData+LINQ+DataTableParser)

LePatayLePatay Posts: 20Questions: 3Answers: 1

Hi,

I'm struggling to add searching/filtering functionalities to a complex ASP.NET project involving an WSDatatable.asmx file and Zack Owens's DataTable parser.
Here is the debug page.

Every functionality of the table works fine except for searching/filtering.
When I type in the search field, this is the error I get is in the DataTableParser.cs file:

System.ArgumentException: An item with the same key has already been added

at this line of the function "FormatedList Parse()":

            list.aaData = _queriable.Where(ApplyGenericSearch)
                                    .Where(IndividualPropertySearch)
                                    .Skip(skip)
                                    .Take(take)
                                    .ToList()
                                    .AsQueryable()
                                    .Select(SelectProperties)
                                    .ToList();

(The original line was slightly modified to suit the need of the project.)

How can I search/filter this DataTable? Is it the fact that I'm getting data through the DataTable parser that makes things impossible to work?

The syntax I use in my WsDataTable.asmx.cs file is:

        [ScriptMethod(UseHttpGet=true, ResponseFormat=ResponseFormat.Json)]
        [WebMethod]
        public FormatedList GetPersonnels()
        {
            FormatedList formatedList;
            HttpRequest request = base.Context.Request;
            using (ISession session = Repository.TSession())
            {
                IQueryable<DTPersonnels> qPersonnel = 
                    from p in session.Query<Personnels>()
                    select new DTPersonnels()
                    {
                        Id = p.Id,
                        NomPersonnel = p.NomPersonnel,
                        Desactive = (p.Desactiver ? "Oui" : "Non")
                    };
                if (request["sEcho"] == null)
                {
                    return null;
                }
                else
                {
                    // When typing in the search field, this line triggers the function "FormatedList Parse()" that errors
                    formatedList = (new DataTableParser<DTPersonnels>(request, qPersonnel)).Parse();
                }
            }
            return formatedList;
        }

Note (useful?): When I use YADCF filter plugin, it only populates the "select" and "autocomplete" with the items of the active page! And it crashes too when I run a search or a filter.

Thanks !

This question has an accepted answers - jump to answer

Answers

  • LePatayLePatay Posts: 20Questions: 3Answers: 1

    By the way, as you can see it in the debug page, I use this initialization:

                var oTable = $('#example').DataTable({
                    "processing": true,
                    "serverSide": true,
                    "stateSave": true,
                    "sAjaxSource": "/WebServices/WSDataTable.asmx/GetPersonnels",
                    "fnServerData": function (sSource, aoData, fnCallback) {
                        $.ajax({
                            "type": "GET",
                            "dataType": 'json',
                            "contentType": "application/json; charset=utf-8",
                            "url": sSource,
                            "data": aoData,
                            "success": function (data) {
                                fnCallback(data.d);
                            }
                        });
                    }
                });
    
  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929

    System.ArgumentException: An item with the same key has already been added

    This is an error caused in the C# code. Sounds like a dictionary issue with duplicate keys. You might get better support on a C# forum like Stack Overflow or a forum specifically for Zack Owens's DataTable parser.

    When I use YADCF filter plugin, it only populates the "select" and "autocomplete" with the items of the active page!

    With serverSide true this is correct. The data available for the the search selections comes from the data at the client which is only the current page.

    Kevin

  • LePatayLePatay Posts: 20Questions: 3Answers: 1
    edited January 2018

    Thank you, Kevin.
    How come this example shows a full table search, then?
    https://datatables.net/examples/data_sources/server_side.html

    I'd like to do the same with my table, but from the moment I start involving the Entity framework in the Ajax call, I stops working.

  • LePatayLePatay Posts: 20Questions: 3Answers: 1

    Any idea?

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929
    Answer ✓

    System.ArgumentException: An item with the same key has already been added

    This seems to indicate duplicate keys are being added. Not knowing C# I'm going to take a guess that the problem is here:

    list.aaData = _queriable.Where(ApplyGenericSearch)
                            .Where(IndividualPropertySearch)
    

    You have Where twice which I think is the cause of the error. I'm guessing ApplyGenericSearch is the global search and IndividualPropertySearch is the individual column search. I'm not sure how to structure the Datatables Parser code to support both at the same time. Someone else on this forum may know but you might find an answer faster in the csharp-datatables-parser issues area.

    Kevin

  • LePatayLePatay Posts: 20Questions: 3Answers: 1

    Thanks Kevin, I will post a feedback here if I make some progress over there!

This discussion has been closed.