Solved

Best Way To Return Database Data To Javascript

Posted on 2011-09-28
7
183 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
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...

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
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.
0
 

Expert Comment

by:andersretteras
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:
http://www.west-wind.com/presentations/jQuery/jQueryPart2.aspx
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Expert Comment

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

Accepted Solution

by:
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
0
 

Author Closing Comment

by:MinnRick
ID: 36954251
See above comment for what I did.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now