0

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 %>
<% } %>
Altin
  • 2,175
  • 3
  • 25
  • 47
  • What are you going to do with that JSON data, render it into a table? If so, a Gridview wraps that functionality up for you. If your intention is to manipulate that dataset client-side then you might be best creating a Javascript endpoint to connect to... – SeanCocteau Mar 06 '12 at 13:55
  • @SeanCocteau thanks for replying. I edited my question above with additional details. – Altin Mar 06 '12 at 14:26

1 Answers1

1

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.

  1. ASP.NET MVC : End to end Jquery, Another example using JsonResult object and a broader example
  2. Create a WCF Service. Here you can create a webservice that can run independantly from your website, however, authentication can be a bind when compared to ASP.NET MVC. Examples are from MSDN, using a simple DataContract (that interprets between .NET objects and what should be outputted (lookup Data Transfer Objects [DTO] for further reading). Here's a walkthrough as well.
  3. Using WebMethod within your code behind. Probably the less impactful for old school ASP.NET developers where you can define methods with the 'Webmethod' attribute that will act as a Javascript Endpoint. It can be a bit fiddly (especially compared to MVC) but may take less time on the implementation. Examples are include simple calls and this and something a bit more complex. One thing I would mention is that you do not get any automatic Json serialising (I think) with this technique and you may have to perform the serialisation yourself as shown in this post.

HTH

Community
  • 1
  • 1
SeanCocteau
  • 1,838
  • 19
  • 25
  • thanks. Today I came across HTTP Handlers and using .ASHX which seems to be exactly what I needed. – Altin Mar 08 '12 at 00:17