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.
// 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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
303 Points
255 Posts
how to return selected query in json format in asp.net web forms
May 08, 2020 03:04 PM|uniservice3|LINK
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
Contributor
3020 Points
893 Posts
Re: how to return selected query in json format in asp.net web forms
May 11, 2020 05:45 AM|Sean Fang|LINK
Hi, uniservice3
Based on your description, I suggest you could do like this, please refer to the following example:
Foreground code:
Behind code(web service):
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