Help in getting data from JSON string from code behind

Help in getting data from JSON string from code behind

srainsrain Posts: 11Questions: 4Answers: 0
edited July 2014 in DataTables 1.10

Hi,
I am just starting out so bear with me.
I got this json string from a webmethod in c# code behind.
And I got this simple initialisation below but it doesn't seem to work?
What am i missing?


$(document).ready(function () { $('#example').dataTable({ "bProcessing":true, "bServerSide": true, "sAjaxSource": "Contact.aspx/DisplayMessage", "sServerMethod": "POST" }); });

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Have you fully implemented server-side processing in your aspx script (required since you have enabled server-side processing)?

    Allan

  • srainsrain Posts: 11Questions: 4Answers: 0

    Isn't this the "bServerSide": true in script as above??

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Yes it is - that is why I asked if you have implemented server-side processing in your aspx script?

  • srainsrain Posts: 11Questions: 4Answers: 0
    edited July 2014

    Sorry Allan, I must be confused or missing something somewhere.

    What I have currently is a json string return from aspx.cs WebMethod.

        public static string DisplayMessage()
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = new DataTable();
            string connstr = "Data Source=localhost;Initial Catalog=xx;User ID=xx;Password=xx@;Connection Timeout=20";
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                SqlCommand sqlComm = new SqlCommand("rTest", conn);
                sqlComm.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = sqlComm;
                da.Fill(dt);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                return serializer.Serialize(rows);
            }
        }
    

    I also have a HTML in aspx of a simple table as this

    <table id="exampless" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>  
                 <th>UserName</th>
                    <th>Completed</th>
                </tr>
            </thead>
           <tbody>
    <tr>
    <td></td>
    <td></td>
    </tr>
    </tbody>
        </table>
    

    And lastly the jquery as first post.
    What am i missing?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    "sAjaxSource": "Contact.aspx/DisplayMessage",

    sAjaxSource expects a JSON data string. That's not what your aspx script is returning.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I'd suggest reading the data source part of the manual.

    Allan

  • srainsrain Posts: 11Questions: 4Answers: 0
    edited July 2014

    Tangerine,

    My aspx json string return is from my code behind of aspx page (in Contact.aspx.cs), in a web method(web method name is DisplayMessage). Here it is:

    [WebMethod]
    public static string DisplayMessage()
    {

        StringBuilder sb = new StringBuilder();
        DataTable dt = new DataTable();
        string connstr = "Data Source=localhost;Initial Catalog=xxx;User ID=xxx;Password=xxx;Connection Timeout=20";
        using (SqlConnection conn = new SqlConnection(connstr))
        {
            SqlCommand sqlComm = new SqlCommand("rTest", conn);
            sqlComm.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = sqlComm;
            da.Fill(dt);
    
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);
        }
    }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I son't see anything there that meeds the requirements for server-side processing, as per the manual page I linked to before. Where is the draw parameter being set for example? Do you really want server-side processing (again, refer to the manual)?

    Allan

  • srainsrain Posts: 11Questions: 4Answers: 0
    edited July 2014

    Hi Allan,

    Thanks for your patience.

    I prefer not to have it to be server-side processing (redraw from server after every refresh of table).
    I would ideally want it to be done in browser.

    However, i thought I HAVE TO make it into server-side processing as I am getting data from my SQL database. Connecting via .cs into a dataset, and converting it into JSON string.

    Maybe the question should rather be, how else can I obtain data from my database to make it work client-side?

    I am using Visual Studio C# and Microsoft SQL.
    Examples are mostly from php so I am confused.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited July 2014

    I prefer not to have it to be server-side processing

    Fair enough. Why do you have "bServerSide": true, in the above code then? :-) That is why I've been talking about server-side processing...

    Maybe the question should rather be, how else can I obtain data from my database to make it work client-side?

    Return the data in a JSON object. How you do that in C#? I'd suggest asking in a C# forum...!

    The DataTables manual has a section about loading data from an object which might also be useful.

    Allan

  • srainsrain Posts: 11Questions: 4Answers: 0

    Hi Allan,
    I managed to convert my data to JSON object, which I called jsonstr.txt.
    I got it working.

    However, it does not work if I don't save it as an object.
    I do not want to have to save each request as an object, rather I prefer to get the data from database, and then display.

    What solution should I be looking at?

This discussion has been closed.