how to return selected query in json format in asp.net web forms RRS feed

  • Question

  • User836525179 posted

    Dear friends, I want to present the report that I take from the database and put in the datatable in the form of json in the web service, but I don't know how to do this.

    in asp.net webforms

    for example when my report is like below

    id                       name                     email

    1                        amir                       un@gmail.com

    2                        zahra                      am@gmail.com


    i want to change this report to json format and return it for web users by web service

    Friday, May 8, 2020 3:04 PM

All replies

  • User-1330468790 posted

    Hi,  uniservice3


    Based on your description, I suggest you could do like this, please refer to the following example:

    Foreground code:

        <form id="userlist_form" runat="server">
                <button id="getUserBtn" type="button">get info</button>
                <br />
                <label id="info"></label>
        <script src="Scripts/jquery-3.4.1.js" type="text/javascript"></script>
            $(document).ready(function () {
                $("#getUserBtn").click(function () {
                        url: "getUserListService.asmx/getUserList",
                        type: "POST",
                        data: null,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d != null) {
                            } else {
                                $("#info").html("There is no data");

    Behind code(web service):

    // datatable to Json
            public string DataTableToJSON(DataTable table)
                string JSONString = string.Empty;
                JSONString = JsonConvert.SerializeObject(table);
                return JSONString;
            public String getUserList()
                String jsonStr;
                DataTable dt = new DataTable();
                String conStr = "Server=.;Database=TestDB;Trusted_Connection=True";
                using (SqlConnection con = new SqlConnection(conStr))
                    String sql = "select * from users";
                    SqlDataAdapter sda = new SqlDataAdapter(sql, con);
                    if (dt.Rows.Count > 0)
                        jsonStr = DataTableToJSON(dt);
                        return jsonStr;
                        return null;


    Page display:



    I would like to know if this can help you meet your needs.

    If it is another requirement, could you please describe it in detail?


    Best regards,


    Monday, May 11, 2020 5:45 AM