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
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
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:
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.


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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

724 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