title: Jquery DataTable with server side using asmx
body: <p>I am using Jquery Datatable 1.10.12 using server side with processing option . I tried the code from their website <a href="https://datatables.net/examples/data_sources/server_side.html" rel="nofollow noreferrer">JQuery Data table server side</a> but without luck . Iam doing something wrong which page is not showing records and gives error 500  Any help would be great. </p>
  
  <p>My code:</p>
  
  <p>.aspxpage   </p>
  
  <pre><code>   &lt;script&gt;
      $(document).ready(function () {
         $('#example').DataTable({
              serverSide: true,
              ajax: 'EmployeeService.asmx/GetPersons'
          });
          });    
        &lt;/script&gt;
      &lt;body&gt;
      &lt;table id="example" class="display" cellspacing="0" width="100%"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;BusinessEntityID&lt;/th&gt;
              &lt;th&gt;FirstName&lt;/th&gt;
              &lt;th&gt;MiddleName&lt;/th&gt;
              &lt;th&gt;LastName&lt;/th&gt;
              &lt;th&gt;PersonType&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;       
      &lt;/table&gt;    
      &lt;/body&gt;
  
    asmx:
      [WebMethod]
      public void GetPersons(int iDisplayLength, int iDisplayStart, int   iSortCol_0, string sSortDir_0, string sSearch )
  
      {
          int displayLength = iDisplayLength;
          int displayStart = iDisplayStart;
          int sortCol = iSortCol_0;
          string  sortDir = sSortDir_0;
          string search = sSearch;
          List&lt;Person&gt; pdetails = new List&lt;Person&gt;();
          int filteredcount = 0;
          using (SqlConnection con = new SqlConnection("Data Source=xxx;Initial Catalog=AdventureWorks;Integrated Security=True"))
          {
              SqlCommand cmd = new SqlCommand(("spGetPersons"), con);
              cmd.CommandType = CommandType.StoredProcedure;
  
              SqlParameter pDisplayLength = new SqlParameter()
              {
                  ParameterName = "@DisplayLength",
                  Value = displayLength
              };
              cmd.Parameters.Add(pDisplayLength);
              SqlParameter pDisplayStart = new SqlParameter()
              {
                  ParameterName = "@DisplayStart",
                  Value = displayStart
              };
              cmd.Parameters.Add(pDisplayStart);
              SqlParameter pSortCol = new SqlParameter()
              {
                  ParameterName = "@SortCol",
                  Value = sortCol
              };
              cmd.Parameters.Add(pSortCol);
              SqlParameter pSortDir = new SqlParameter()
              {
                  ParameterName = "@SortDir",
                  Value = sortDir
              };
              cmd.Parameters.Add(pSortDir);
              SqlParameter pSearchString = new SqlParameter()
              {
                  ParameterName = "@Search",
                  Value = string.IsNullOrEmpty(search)?null:search
              };
              cmd.Parameters.Add(pSearchString);
              con.Open();
              SqlDataReader rdr = cmd.ExecuteReader();
              while (rdr.Read())
              {
                  Person per = new Person();
                  per.BusinessEntityID = rdr["BusinessEntityID"].ToString();
                  filteredcount = Convert.ToInt32(rdr["TotalCount"].ToString());
                  per.FirstName = rdr["FirstName"].ToString();
                  per.MiddleName = rdr["MiddleName"].ToString();
                  per.LastName = rdr["LastName"].ToString();
                  per.PersonType = rdr["PersonType"].ToString();
                  pdetails.Add(per);
              }
          }
  
          var result = new
          {
  
              draw = 1, 
              recordsTotal = GetPersonsTotalCount(),
              recordsFiltered = filteredcount,
              data = pdetails
          };
  
          JavaScriptSerializer js = new JavaScriptSerializer();
          js.MaxJsonLength = 2147483644;
         Context.Response.Write(js.Serialize(result));
      }
  
      private int GetPersonsTotalCount()
      {
          int totalPersonCount = 0;
          using (SqlConnection con = new SqlConnection("Data Source=xxx;Initial Catalog=AdventureWorks;Integrated Security=True"))
          {
              SqlCommand cmd = new SqlCommand(("select count(*) from Person.Person"), con);
              con.Open();
              totalPersonCount = (int)cmd.ExecuteScalar();
          }
          return totalPersonCount;
      }
      public class Person
      {
          public string BusinessEntityID { get; set; }
          public string FirstName { get; set; }
          public string MiddleName { get; set; }
          public string LastName { get; set; }
          public string PersonType { get; set; }
  
      }
  
     JSON :
  
    {"draw":1,"recordsTotal":19972,"recordsFiltered":19972,"data":[{"BusinessEntityID":"1","FirstName":"Ken","MiddleName":"J","LastName":"Sánchez","PersonType":"EM"},{"BusinessEntityID":"2","FirstName":"Terri","MiddleName":"Lee","LastName":"Duffy","PersonType":"EM"},{"BusinessEntityID":"3","FirstName":"Roberto","MiddleName":"","LastName":"Tamburello","PersonType":"EM"},{"BusinessEntityID":"4","FirstName":"Rob","MiddleName":"","LastName":"Walters","PersonType":"EM"},{"BusinessEntityID":"5","FirstName":"Gail","MiddleName":"A","LastName":"Erickson","PersonType":"EM"},{"BusinessEntityID":"6","FirstName":"Jossef","MiddleName":"H","LastName":"Goldberg","PersonType":"EM"},{"BusinessEntityID":"7","FirstName":"Dylan","MiddleName":"A","LastName":"Miller","PersonType":"EM"},{"BusinessEntityID":"8","FirstName":"Diane","MiddleName":"L","LastName":"Margheim","PersonType":"EM"},{"BusinessEntityID":"9","FirstName":"Gigi","MiddleName":"N","LastName":"Matthew","PersonType":"EM"},{"BusinessEntityID":"10","FirstName":"Michael","MiddleName":"","LastName":"Raheem","PersonType":"EM"}]}
  </code></pre>
  
  <p>ERROR :</p>
  
  <blockquote>
    <p><a href="http://localhost:64400/EmployeeService.asmx/GetPersons?draw=1&amp;columns%5B0%5" rel="nofollow noreferrer">http://localhost:64400/EmployeeService.asmx/GetPersons?draw=1&amp;columns%5B0%5</a>…art=0&amp;length=10&amp;search%5Bvalue%5D=&amp;search%5Bregex%5D=false&amp;_=1478471333896
    Failed to load resource: the server responded with a status of 500
    (Internal Server Error) Server Error in '/' Application.</p>
    
    <p>Request format is unrecognized for URL unexpectedly ending in
    '/GetPersons'.</p>
    
    <p>Description: An unhandled exception occurred during the execution of
    the current web request. Please review the stack trace for more
    information about the error and where it originated in the code. </p>
    
    <p>Exception Details: System.InvalidOperationException: Request format is
    unrecognized for URL unexpectedly ending in '/GetPersons'.</p>
    
    <p>Source Error: </p>
    
    <p>An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of
    the exception can be identified using the exception stack trace below.</p>
    
    <p>Stack Trace: </p>
    
    <p>[InvalidOperationException: Request format is unrecognized for URL
    unexpectedly ending in '/GetPersons'.]<br>
    System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type
    type, HttpContext context, HttpRequest request, HttpResponse response)
    +169669    System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext
    context, String verb, String url, String filePath) +209<br>
    System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext
    context, String requestType, String url, String pathTranslated) +120<br>
    System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    +339    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155</p>
  </blockquote>