2013-02-04 64 views
0

我正在使用Jqgrid在asp.net中使用web服務將我的數據顯示到jqgrid中......但它只在我給查詢限制時加載。如果我想在代碼中使用第二個commneted查詢加載所有數據,那麼它不會加載並給出錯誤「」Message「:」使用JSON JavaScriptSerializer進行序列化或反序列化時出錯。字符串的長度超過上maxJsonLength屬性設置的值。」數據不顯示在Jqgrid在Asp.net中使用Web服務

這是我的完整模塊架構...

的Index.aspx頁...

<script type="text/javascript"> 
    $(function() { 
     $("#table").jqGrid({ 
      datatype: function (pdata) { getData(pdata); }, 
      height: 500, 
      colNames: ['username', 'ordinal', 'authcode', 'extension', 'trunk', 'dialnumber', 'dialdate', 'dialtime', 'duration', 'destination', 'price', 'toc'], 
      colModel: [ 
        { name: 'username', width: 100, sortable: true, align: 'center' }, 
        { name: 'ordinal', width: 100, sortable: true, align: 'center' }, 
        { name: 'authcode', width: 100, sortable: true }, 
        { name: 'extension', width: 100, sortable: true, align: 'center' }, 
        { name: 'trunk', width: 100, sortable: true, align: 'center' }, 
        { name: 'dialnumber', width: 100, sortable: true, align: 'center' }, 
        { name: 'dialdate', width: 100, sortable: true, align: 'center' }, 
        { name: 'dialtime', width: 100, sortable: true, align: 'center' }, 
        { name: 'duration', width: 100, sortable: true, align: 'center' }, 
        { name: 'destination', width: 100, sortable: true, align: 'center' }, 
        { name: 'price', width: 100, sortable: true, align: 'center' }, 
        { name: 'toc', width: 100, sortable: true, align: 'center' } 
       ], 
      rowNum: 100, 
      rowList: [100, 200, 300], 
      pager: '#UsersGridPager', 
      sortname: 'username', 
      sortable: true, 
      viewrecords: true, 
      sortorder: 'asc', 
      shrinkToFit: false, 
      rownumbers: true, 
      loadtext: 'Loading..' 

     }); 
     jQuery("#table").jqGrid('navGrid', '#UsersGridPager', { add: false, edit: false, del: false, search: true, refresh: true }); 
    }); 


    function getData(pData) { 
     $.ajax({ 
      type: 'POST', 
      contentType: "application/json; charset=utf-8", 
      url: '<%= ResolveClientUrl("~/WebService.asmx/GetListOfPersons") %>', 
      data: '{}', 
      dataType: "json", 
      success: function (data, textStatus) { 
       if (textStatus == "success") 
        ReceivedClientData(JSON.parse(getMain(data)).rows); 
      }, 
      error: function (data, textStatus) { 
       alert('An error has occured retrieving data!'); 
      } 
     }); 
    } 


    function ReceivedClientData(data) { 
     var thegrid = $("#table"); 
     thegrid.clearGridData(); 
     for (var i = 0; i < data.length; i++) 
      thegrid.addRowData(i + 1, data[i]); 
    } 


    function getMain(dObj) { 
     if (dObj.hasOwnProperty('d')) 
      return dObj.d; 
     else 
      return dObj; 
    } 
</script> 

JsonHelper.cs文件

// Convert Object to Json String 
// <param name="obj">The object to convert</param> 
// <returns>Json representation of the Object in string</returns> 

public static string ToJson(object obj) 
{ 
    return Newtonsoft.Json.JsonConvert.SerializeObject(obj); 
} 

public static List<Person> GetPersons() 
{ 
    List<Person> persons = new List<Person>(); 
    string connectionString = "Server=localhost;Port=3306;Database=projecttt;UID=root;Pwd=techsoft;pooling=false"; 

    MySqlConnection conn; 
    conn = new MySqlConnection(connectionString); 
    conn.Open(); 

    string s = "SELECT username,ordinal,authcode,extension,trunk,dialnumber,dialdate,dialtime,duration,destination,price,toc FROM processeddata_table order by username limit 0,200"; 
    // string s = "SELECT username,ordinal,authcode,extension,trunk,dialnumber,dialdate,dialtime,duration,destination,price,toc FROM processeddata_table "; 
    MySqlCommand cmd = new MySqlCommand(s,conn); 

    cmd.ExecuteNonQuery(); 

    using (MySqlDataReader dr = cmd.ExecuteReader()) 
    { 

     while (dr.Read()) 
     { 
      persons.Add(new Person() 
      { 
       username = Convert.ToString(dr["username"]), 
       ordinal = Convert.ToString(dr["ordinal"]), 
       authcode = Convert.ToString(dr["authcode"]), 
       extension = Convert.ToString(dr["extension"]), 
       trunk = Convert.ToString(dr["trunk"]), 
       dialnumber = Convert.ToString(dr["dialnumber"]), 
       dialdate = Convert.ToString(dr["dialdate"]), 
       dialtime = Convert.ToString(dr["dialtime"]), 
       duration = Convert.ToString(dr["duration"]), 
       destination = Convert.ToString(dr["destination"]), 
       price = Convert.ToString(dr["price"]), 
       toc = Convert.ToString(dr["toc"]) 

      }); 
     } 

    } 

    return persons; 
} 

}

PagedList.cs文件

IEnumerable _rows; 
int _totalRecords; 
int _pageIndex; 
int _pageSize; 
object _userData; 

public PagedList(IEnumerable rows, int totalRecords, int pageIndex, int pageSize, object userData) 
{ 
    _rows = rows; 
    _totalRecords = totalRecords; 
    _pageIndex = pageIndex; 
    _pageSize = pageSize; 
    _userData = userData; 
} 

public PagedList(IEnumerable rows, int totalRecords, int pageIndex, int pageSize) 
    : this(rows, totalRecords, pageIndex, pageSize, null) 
{ 
} 

public int total { get { return (int)Math.Ceiling((decimal)_totalRecords/(decimal)_pageSize); } } 

public int page { get { return _pageIndex; } } 

public int records { get { return _totalRecords; } } 

public IEnumerable rows { get { return _rows; } } 

public object userData { get { return _userData; } } 

public override string ToString() 
{ 
    return Newtonsoft.Json.JsonConvert.SerializeObject(this); 
} 

}

webservices.cs

[WebMethod] 
[ScriptMethod] 

public string GetListOfPersons() 
{ 
    List<Person> persons = JsonHelper.GetPersons(); 
    return Newtonsoft.Json.JsonConvert.SerializeObject(new PagedList(persons, persons.Count, 1, persons.Count)); 
} 

}

Person.cs

public string username { get; set; } 
public string ordinal { get; set; } 
public string authcode { get; set; } 
public string extension { get; set; } 
public string trunk { get; set; } 
public string dialnumber { get; set; } 
public string dialdate { get; set; } 
public string dialtime { get; set; } 
public string duration { get; set; } 
public string destination { get; set; } 
public string price { get; set; } 
public string toc { get; set; } 

PLZ人幫我.THanx提前..

回答

0

你爲什麼選擇ExecuteNonQuery()?嘗試ExecuteReader()方法。

看到這個: - Question on MaxJsonLength

對於本地分頁loadonce:真正可以使用,但仍然因爲你要加載它,不建議過多的數據,你會得到同樣的錯誤。此外,阿賈克斯請求用於處理少量的數據

+0

@克里斯thnx先生爲你的反應..我改變了它,但問題是相同 – vikas

+0

什麼是在Firefox的控制檯或鉻開發人員控制檯的JSON響應? – Kris

+0

先生,如果我正在執行有限的記錄,如極限100,然後它被填充在第一頁,結果是在控制檯...但如果我與查詢超出限制,然後它試圖加載所有記錄同時並給出錯誤「」Message「:」使用JSON JavaScriptSerializer進行序列化或反序列化時出錯。字符串的長度超過了maxJsonLength屬性設置的值。「,」StackTrace「:」「 – vikas