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

  • Question

  • User836525179 posted

    Hi
    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:

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

    Behind code(web service):

    // datatable to Json
    
            public string DataTableToJSON(DataTable table)
    
            {
    
                string JSONString = string.Empty;
    
                JSONString = JsonConvert.SerializeObject(table);
    
                return JSONString;
    
            }
    
             
    
            [WebMethod]
    
            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);
    
                    sda.Fill(dt);
    
                    if (dt.Rows.Count > 0)
    
                    {
    
                        jsonStr = DataTableToJSON(dt);
    
                        return jsonStr;
    
                    }else
    
                    {
    
                        return null;
    
                    }
    
                }
    
            }

    Result:

    Page display:

    Response:

     

    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,

    Sean

    Monday, May 11, 2020 5:45 AM