Reading Database value in Javascript

Today while developing a page, we felt need to read database into javascript array. And we were confused if at all we can do this. So finally after googling on this we found a workaround. So I just thought of sharing this with you because I think this is really helpful.

[code]<script language="JavaScript" type="text/javascript">
var allRecords = new array();
var count=0;
<%While (NOT rs_yours.EOF)%>
allrecords[count]="<%= rs_yours.Fields.Item("foo1").Value %>"

This script will read the value from your datareader into javascript array.

Also if you want to read a single value from the server side into your javascript like this,

[code]var location='<%=Session["location"]%>’;[/code]

Hope this helps 🙂

3 thoughts on “Reading Database value in Javascript

  1. Evans

    It is funny, you just showed a small piece of Javascript for looping through an Array, but nowhere in that code did you show how to read database.

    In fact, I will post a sample code on my blog to show you how you could do it if you’re struggling with it.

    1. salviashish

      Hey Evans,
      I just demonstrated how can we access server side variables / recordset which are most often handled in code behind.
      I am aware of using ActivexObjects, would like to know is there any other alternative, looking forward for your post.
      Please drop a link of the post when you are done.


  2. Pingback: Reading Database value in Javascript « Beautiful Mind

Leave a Reply

Your email address will not be published. Required fields are marked *