Solved

Best Way To Return Database Data To Javascript

Posted on 2011-09-28
7
180 Views
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
0
Comment
Question by:MinnRick
7 Comments
 
LVL 96

Expert Comment

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

Proof-of-concept:

Calling a static "page method" from Javascript using MS AJAX
http://www.geekzilla.co.uk/View7B75C93E-C8C9-4576-972B-2C3138DFC671.htm
0
 
LVL 1

Expert Comment

by:danbaugher
Comment Utility
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.
0
 

Expert Comment

by:andersretteras
Comment Utility
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:
http://www.west-wind.com/presentations/jQuery/jQueryPart2.aspx
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:MinnRick
Comment Utility
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
0
 
LVL 1

Expert Comment

by:danbaugher
Comment Utility
JSON is probably your best option if you're loading data that needs to be manipulated. Post your problematic code for a better answer.
0
 

Accepted Solution

by:
MinnRick earned 0 total points
Comment Utility
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
0
 

Author Closing Comment

by:MinnRick
Comment Utility
See above comment for what I did.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now