If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > sql recordset into javascript array

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-04, 09:26
plork123 plork123 is offline
Registered User
 
Join Date: Jan 2004
Posts: 53
Angry sql recordset into javascript array

Can someone help with this little one, i reallly am stuck

I want to produce an array for tree nodes on a page

I connect to sql fine but i'm, have trouble going through a for loop

function getRecordset(sql)
{
var dbConnectionString = "DRIVER={SQL SERVER};SERVER=name;DATABASE=name;UID=sa;PWD=passw ord"
var sDBConnection = new ActiveXObject("ADODB.Connection");
sDBConnection.Open (dbConnectionString);

var rs = new ActiveXObject("ADODB.Recordset");
rs.CursorLocation = 3;
rs.CursorType = 3;
rs.LockType = 1;
rs.Open(sql, sDBConnection);

return rs;

rs.Close;
}

var rs = getRecordset("SELECT * FROM mytable");

mytable has 2 cols 'id' and 'name' and 2 rows - a simple table to get me started

e.g
id name
1 my first node
2 my second node


I want to use something like this, but don't know how to finish it off

How do I get the numberOfRows? Is it rs.recordcount?
How do I get the numberOfColumns?
I don't understand the "queryResults.rows[i].columns[j] +" part

for (var i=0; i<numberOfRows; i++)
{
document.write(i > 0 ? ",\n[" : "[");

for (var j = 0; j < numberOfColumns; j++)
{
document.write
(
(j > 0 ? ", " : "") +
"'" +
queryResults.rows[i].columns[j] +
"'"
);
}
document.write(" ]");
}

many thanks
Reply With Quote
  #2 (permalink)  
Old 09-08-04, 10:52
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
Hi,
it looks like you are mixing server side javacasript and client side.

First if you want to loop through your entire recordset it is easier to use a while loop. Then i am not too sure why you wnat to loop throught the column, you already know your column from the database, all you have to do is use instances of your rs:

Code:
//looping through the recorset until we reach the last record:

while (rs.EOF == false)//EOF stands for end of file
{
//lets get the value of the first column to use in whatever way you want and output ot the page
//note that document.write is replaced by Response.Write in server side JS

Response.Write("Here is the Id: "+rs("id").Value+" and here is the name: "+rs("name").Value+"");

//then we move to the next record
rs.MoveNext();


}
//now all records in your DB are printed on the screen and we are outside the loop
Hope this helps,
Olivier
Reply With Quote
  #3 (permalink)  
Old 09-08-04, 11:24
plork123 plork123 is offline
Registered User
 
Join Date: Jan 2004
Posts: 53
I need the recordset to be an array so i can pass it to other code

like this
<script type="text/javascript">
var rows = [
<%
for (var i = 0; i < numberOfRows; i++) {
Response.write(i > 0 ? ",\n[" : "[");
for (var j = 0; j < numberOfColumns; j++) {
Response.write(
(j > 0 ? ", " : "") +
"'" +
queryResults.rows[i].columns[j] +
"'"
);
}
Response.write(" ]");
}
%>
];
</script>

Thanks
Reply With Quote
  #4 (permalink)  
Old 09-09-04, 08:09
oliflorence oliflorence is offline
Registered User
 
Join Date: Aug 2004
Posts: 96
it looks like you are mixing up server side and client side javacsript.

To fill up an array in client side JS with data from your database, you would do something like this:


Code:
<script language="javascript">

//opening an array

var MyArray = new Array();

//now we go server side to get the data from the db
<%
while (rs.EOF == false)
{

%>
//now going back client side
//let say than the index of your array will be the id from your db
//and the value of your array will be the names


//using <%=  replaces your response.Write, you only use Response.Write to write on the page, not to pass a value
MyArray[<%= rs("id").Value %>] = <%= rs("name").Value %>;

<%
//now we go back server side to keep looping
//then we move to the next record
rs.MoveNext();


}



%>




</script>
This will store all values from your db into your array

Hope this helps
Reply With Quote
  #5 (permalink)  
Old 09-10-04, 13:05
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
plork.. is this Server-Side JavaScript (SSJS) or VBScript?
__________________
That which does not kill me postpones the inevitable.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On