im using ASP.NEt to make a simple database query and return JSON formated data.
Right now I have the following code which displays the results from database:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="ScanId" HeaderText="ScanId" ReadOnly="True"
SortExpression="ScanId" />
<asp:BoundField DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
<asp:BoundField DataField="barcode" HeaderText="barcode"
SortExpression="barcode" />
<asp:BoundField DataField="latitude" HeaderText="latitude"
SortExpression="latitude" />
<asp:BoundField DataField="longitude" HeaderText="longitude"
SortExpression="longitude" />
<asp:BoundField DataField="date_time" HeaderText="date_time"
SortExpression="date_time" />
<asp:BoundField DataField="locatio_name" HeaderText="locatio_name"
SortExpression="locatio_name" />
<asp:BoundField DataField="pos_accuracy" HeaderText="pos_accuracy"
SortExpression="pos_accuracy" />
<asp:BoundField DataField="pos_country" HeaderText="pos_country"
SortExpression="pos_country" />
<asp:BoundField DataField="pos_territory" HeaderText="pos_territory"
SortExpression="pos_territory" />
<asp:BoundField DataField="pos_city" HeaderText="pos_city"
SortExpression="pos_city" />
<asp:BoundField DataField="pos_street" HeaderText="pos_street"
SortExpression="pos_street" />
<asp:BoundField DataField="speed" HeaderText="speed" SortExpression="speed" />
<asp:BoundField DataField="course" HeaderText="course"
SortExpression="course" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:dbConnectionString1.ProviderName %>"
SelectCommand="SELECT [ScanId], [UserId], [barcode], [latitude], [longitude], [date_time], [locatio_name], [pos_accuracy], [pos_country], [pos_territory], [pos_city], [pos_street], [speed], [course] FROM [ScanDetails] WHERE [UserId] = '1'">
</asp:SqlDataSource>
Can I use this to get data JSON formated? Can you point me in the right direction?
EDIT
I have a mobile app that loads the .aspx which returns JSON data to the app. So I basically need some sort of Response.Write(json_data);
I dont know how to implement that, although I’m searching for possible solutions all day long.
I managed to use ADO.NET Entity Data Model and to make queries this way:
in controller i did:
public class ReadController : Controller
{
dbEntities1 _db = new dbEntities1();
//
// GET: /Read/
public ActionResult Index()
{
ViewBag.myData = from c in _db.users select c;
return View();
}
}
in view i did:
<% foreach (scan_barcode2sql_com.Models.user c in (IEnumerable)ViewBag.myData)
{ %>
<%= c.username %>
<% } %>
Just for completeness…
The Gridview renders out a HTML table and while it is accessible by Jquery / Javascript to harvest its data, it is easier to simply create an endpoint within your ASP.NET application that can return JSON data. Here are some approaches, my personal favourite would be jQuery to ASP.NET MVC as you can create a controller to handle and render HTML output and JSON data to same model.
HTH