Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Best Way To Return Database Data To Javascript

Posted on 2011-09-28
Last Modified: 2012-05-12
Hello Experts -
I have some javascript running in an ASP.NET page that requires some data that's resident in a database.  I need the data in any of three forms:  an array, XML or JSON.  What is the least complicated means to accomplish this?  The digging that I've done thus far has me thinking that jQuery's .load() may do the trick, with a target page set to make the query and output the data, either in XML or JSON.  Does this sound right?  If not, what would be better?  If so, can someone please show me how to code a simple ASP.NET (VB.NET) page to render XML or JSON, and then how to code the javascript on the first page to consume it?  I've looked at what seems like a thousand Googled pages and can't seem to find a simple VB.NET-coded example.  Thanks!

-- Rick
Question by:MinnRick
LVL 96

Expert Comment

by:Bob Learned
ID: 36768822
I would look at page methods with the AJAX ToolScriptManager, where you could use code-behind to get the data in your JavaScript code...


Calling a static "page method" from Javascript using MS AJAX

Expert Comment

ID: 36810155
XML or JSON would work it all depends on what kind of data you need to pull in. Do you need to manipulate the data with JavaScript or is the data already in the correct format? Could you format the data with ASP instead and just output the HTML from the ASP code? You need more information for a better answer.

Expert Comment

ID: 36813229
My best suggestion and the way I normally fetch data from a database to javascript is by using jQuery ajax callbacks to a WCF service (using a serviceProxy) which returns data via JSON.

It might sound a little too much for your scenario at the moment, but doing it this way you will establish a platform of fetching server side data for future projects as well.

Have a look at Rick Strahl's posts here:
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 36816122
I do need to manipulate the data into a javascript array if that's the route I end up with.

The use case, by the way, is to grab some SQL Server data to display in JSCharts.  Their documentation on this is limited pretty much to configuring the chart object.  If anyone can assist with some sample code that gathers the data from the client side and then drops it into a JSChart that'd be ideal.  Thanks again!

-- Rick

Expert Comment

ID: 36817745
JSON is probably your best option if you're loading data that needs to be manipulated. Post your problematic code for a better answer.

Accepted Solution

MinnRick earned 0 total points
ID: 36930753
Thank you again everyone.  I ended up going with an array-based solution for this.  I built a second page that retrieves and response.writes the data into a comma-delimited string.  I then used a jQuery GET from the original page to retrieve and parse (split function) the data from the GET and then iterate through the parsed data to build (push function) the array(s) needed for the JSChart call.  Simple and works great.

-- Rick

Author Closing Comment

ID: 36954251
See above comment for what I did.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question