forum

Home / DeveloperSection / Forums / DataTables in ASP.net using JSON

DataTables in ASP.net using JSON

marcel ethan298409-Dec-2014

Using JQuery DataTables in ASP.net and trying to return JSON data to it from C# WebMethod. 

C# method... 

[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Data()
{
    string json =string.Empty;
 
    using (IDataReader reader = DBUtil.GetReader("data", "data", "pram"))
    {
        if (reader !=null)
        {
            DataTable dt = new DataTable();
            dt.Load(reader);
            reader.Close();
            json =JsonHelper.GetJsonFromDataTable(dt);
        }
    }
    return json;   //also tried by adding "{\"aaData\": " + json + "}"
}

Working.... 

var source = {};
$.ajax({
    type: 'POST',
    dataType: 'json',
    url: "page.aspx/Data",
    contentType: 'application/json; charset=utf-8',
    cache: false,
    data: {},
    success: function (response) {
        source = $.parseJSON(response.d);
        alert(response.d); // i can see Json formatted data 
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
    }
});

Not working....... (not even hitting the method break point) 

$(".JGrid").dataTable({
    "bJQueryUI": true,
    "sAjaxSource": "../page.aspx/Data", //also tried with var 'source' (declared sbove)
    "aoColumns": [
        { "sTitle": "Col1name", "sWidth": "33%" },
        { "sTitle": "Col2name", "sWidth": "33%" },
        { "sTitle": "Col3name", "sWidth": "33%" }
    ]
});


Updated on 10-Dec-2014

Can you answer this question?


Answer

2 Answers

Liked By