[Solved] maxJsonLength error using jQuery AJAX in ASP.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

I have very large amount data in c# which are take more time for proccessing and it was given exception also when max string length cross i want any to partition the data in small small peice and process and send it to ajax response  using web api c#

I have these code  I want to split data into multiple part and send it one by one part on ajax response but display when all data recive by ajax 

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using WebApplication2.Models;

namespace WebApplication2.apiController
{
    public class com_serialize_dataController : ApiController
    {
        [HttpPost]
        public string  Employee()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[9] {
                new DataColumn("Id", typeof(int)),
                new DataColumn("Name", typeof(string)),
                new DataColumn("Address",typeof(string)),
                new DataColumn ("Phone",typeof(string)),
                new DataColumn ("Email", typeof(string)),
                new DataColumn ("Project", typeof(string)),
                new DataColumn("Role",typeof(string)),
                new DataColumn ("Status",typeof(string)),
                new DataColumn("Completion",typeof(string))

            });

            for (int i = 0; i < 1000; i++)
            {
                dt.Rows.Add(i, "Ashok We've also got a Draw counter. This is important, because DataTables ke  var j = 1;eps a count of the number of times it 00redraws the table. Every time you interact with thej++table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Faridabad We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "0654546644 We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "ashok@gmail.com We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Signage We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Devloper We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Progress We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "02/12/2019 We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side");
            }
           
            var splitdata = dt.AsEnumerable().Take(200).CopyToDataTable();
            return JsonConvert.SerializeObject(splitdata);
        }
    }
}

 

    <script type="text/javascript">
        $(document).ready(function () {
            // var myjson = JSON.stringify(Employee);
            $('#btn').on('click', function () {
                $.ajax({
                    type: 'Post',
                    url: "/api/com_serialize_data",
                    // data: myjson,
                    //dataType: 'json',
                    contentType: "application/json; charset=utf-8;Content-Encoding=gzip",
                    //  ContentEncoding: gzip,
                    success: function (response) {

                        var employees = JSON.parse(response);
                        var tr;
                        for (var i = 0; i < employees.length; i++) {

                            tr = $('<tr/>');

                            tr.append("<td>" + employees[i].Id + "</td>");
                            tr.append("<td>" + employees[i].Name + "</td>");
                            tr.append("<td>" + employees[i].Address + "</td>");
                            tr.append("<td>" + employees[i].Phone + "</td>");
                            tr.append("<td>" + employees[i].Email + "</td>");
                            tr.append("<td>" + employees[i].Project + "</td>");
                            tr.append("<td>" + employees[i].Role + "</td>");
                            tr.append("<td>" + employees[i].Status + "</td>");
                            tr.append("<td>" + employees[i].Completion + "</td>");
                            $('#tbl').append(tr);
                            $('tr')
                        }
                    },

                    error: function (errorThrown) {
                        alert(errorThrown);
                    }
                });
            });
        });
    </script>

 

    <center>
        <input type="button" id="btn" value="Get Data" />
        <div>
            <table id="tbl">
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Address</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>Project</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th>Completion</th>
                </tr>
            </table>
            <!--<div id="output"></div>-->
        </div>
    </center>
You are viewing reply posted by: pandeyism 7 months ago.
Posted 7 months ago

Hi sanjay8090,

Refer below sample.

And refer below link to resolve your error.

ASP.Net AJAX Error: The length of the string exceeds the value set on the maxJsonLength property

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btn').on('click', function () {
                $.ajax({
                    type: 'Post',
                    url: "CS.aspx/Employee",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        employees = JSON.parse(response.d);
                        var tr;
                        for (var i = 0; i < employees.length; i++) {
                            tr = $('<tr/>');
                            tr.append("<td>" + employees[i].Id + "</td>");
                            tr.append("<td>" + employees[i].Name + "</td>");
                            tr.append("<td>" + employees[i].Address + "</td>");
                            tr.append("<td>" + employees[i].Phone + "</td>");
                            tr.append("<td>" + employees[i].Email + "</td>");
                            tr.append("<td>" + employees[i].Project + "</td>");
                            tr.append("<td>" + employees[i].Role + "</td>");
                            tr.append("<td>" + employees[i].Status + "</td>");
                            tr.append("<td>" + employees[i].Completion + "</td>");
                            $('#tbl').append(tr);
                            $('tr')
                        }
                    },
                    error: function (errorThrown) {
                        debugger;
                        alert(errorThrown);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <center>
        <input type="button" id="btn" value="Get Data" />
        <div>
            <table id="tbl">
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        Address
                    </th>
                    <th>
                        Phone
                    </th>
                    <th>
                        Email
                    </th>
                    <th>
                        Project
                    </th>
                    <th>
                        Role
                    </th>
                    <th>
                        Status
                    </th>
                    <th>
                        Completion
                    </th>
                </tr>
            </table>
        </div>
    </center>
    </form>
</body>
</html>

Namespaces

C#

using System.Data;
using Newtonsoft.Json;
using System.Web.Services;

VB.Net

Imports System.Data
Imports Newtonsoft.Json
Imports System.Web.Services

Code

C#

[WebMethod]
public static string Employee()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[9] {
        new DataColumn("Id", typeof(int)),
        new DataColumn("Name", typeof(string)),
        new DataColumn("Address",typeof(string)),
        new DataColumn ("Phone",typeof(string)),
        new DataColumn ("Email", typeof(string)),
        new DataColumn ("Project", typeof(string)),
        new DataColumn("Role",typeof(string)),
        new DataColumn ("Status",typeof(string)),
        new DataColumn("Completion",typeof(string))
 
    });

    for (int i = 0; i < 1000; i++)
    {
        dt.Rows.Add(i, "Ashok We've also got a Draw counter. This is important, because DataTables ke  var j = 1;eps a count of the number of times it 00redraws the table. Every time you interact with thej++table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Faridabad We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "0654546644 We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "ashok@gmail.com We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Signage We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Devloper We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Progress We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "02/12/2019 We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side");

    }
    var splitdata = dt.AsEnumerable().CopyToDataTable();
    return JsonConvert.SerializeObject(splitdata);
}

VB.Net

<WebMethod()>
Public Shared Function Employee() As String
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(8) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Address", GetType(String)), New DataColumn("Phone", GetType(String)), New DataColumn("Email", GetType(String)), New DataColumn("Project", GetType(String)), New DataColumn("Role", GetType(String)), New DataColumn("Status", GetType(String)), New DataColumn("Completion", GetType(String))})

    For i As Integer = 0 To 1000 - 1
        dt.Rows.Add(i, "Ashok We've also got a Draw counter. This is important, because DataTables ke  var j = 1;eps a count of the number of times it 00redraws the table. Every time you interact with thej++table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Faridabad We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "0654546644 We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "ashok@gmail.com We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Signage We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Devloper We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "Progress We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side", "02/12/2019 We've also got a Draw counter. This is important, because DataTables keeps a count of the number of times it redraws the table. Every time you interact with the table, DataTables fetches the data again from the server. It also increments that counter. When the call comes back with the data, the Draw parameter needs to be there with that same value or we'll get an error. You can read more about the parameters here: DataTables: Server Side")
    Next

    Dim splitdata = dt.AsEnumerable().CopyToDataTable()
    Return JsonConvert.SerializeObject(splitdata)
End Function