reference :- https://www.c-sharpcorner.com/article/how-to-convert-and-bind-json-string-in-to-asp-net-gridview-with-c-sharp/
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
String conStr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand("Select * from aggregate._form_info", con))
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
string json = JsonConvert.SerializeObject(dt);
return json;
//convert from JSON to object
//DataTable myObjectDT = JsonConvert.DeserializeObject<DataTable>(json);
// JavaScriptSerializer js1 = new JavaScriptSerializer();
//return JsonConvert.SerializeObject(myObjectDT);
//without returning function, below code
//Context.Response.Write(json);
con.Close();
}
}
}
-------------------**************------------------------
web.config connection string
<configuration>
<connectionStrings>
<add name="connectionString" connectionString="initial catalog=aggregate;user=sa1;password=sa;data source=." />
</connectionStrings>
</configuration>
--------------------*************************--------------------------------
Output in JSON
No comments:
Post a Comment