Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript: Get number of rows of a recordset

Status
Not open for further replies.

frohberg

Programmer
Oct 20, 2003
5
DE
Hello.

I need to get the number of rows from a recordset. I use the "RecordCount" property of the Recordset object, but the number is -1 even though in MS Query Analyzer the number of records is 1.

Here my JavaScript code:

var con = new ActiveXObject("ADODB.Connection");
con.Open("Provider=SQLOLEDB;SERVER=dflaptop;UID=sa;PWD=;Trusted_Connection=yes;");

var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT name FROM master.dbo.sysdatabases WHERE name = \'PTC\'", con)

var x = rs.RecordCount;

alert(x);


The statement in Query Analyzer is exactly the same - 1 record returned.

Anybody can help me?
 
Are you running the Query Analyzer after the whole script runs? Is the record count changing before or after your snippet of code to pull the number of records? Put a Response.End; right after your rs.Open statement and check the database then thru Query Analyzer. See if it provides -1 at that point in time, as that is what your alert statement is providing.

A quick question.... I thought all database commands had to be done server side, but alert is a client side function. Am I missing something here?

Also..... why would there ever be a negative record count? Also another situation I've never run into I guess.

-kaht
 
remove the \ in ur sql. change \'PTC\' to 'PTC'...

Known is handfull, Unknown is worldfull
 
@vbkris

The same return value after I've remove the "\" in the statement. Records are still "-1".

@kaht

I run the Query Analyzer after and before the javascript (for testing) - same effect. The database exists definitly.
But I don't understand the "Response.End" command - what should I write for example?

The alert command is only for me to see what it happens. All the code runs locally on my pc - client side.

 
Response.End is an asp command. When you were talking about accessing the database I just assumed you were using some server side language.

Response.End just makes your program stop. So I figured if you were getting 1 row in the query analyzer before/after you run the page, but -1 when you run the SELECT statement, that your database was being manipulated somewhere before/after you are pulling the records. A Response.End would have halted the program right after you ran the SELECT statement so you could check it in query analyzer at that time, to make sure you weren't getting 1. That would imply that your code was manipulating the database somewhere else.

-kaht
 
Hi again!

Ahh, yes - Response.End and ASP. But I write a client-side script, I use only JavaScript. This side should run on every machine with sql server on it, an webserver like IIS is not installed.

But I have managed the problem:

var con = new ActiveXObject("ADODB.Connection");
con.Open("Provider=SQLOLEDB;SERVER="+Host+";UID=sa;PWD=;Trusted_Connection=yes;");

var rs = new ActiveXObject("ADODB.Recordset");
rs = con.Execute("SELECT name FROM master.dbo.sysdatabases WHERE name = 'PTC'");

if (rs.EOF == true)
{
con.Execute(sql1);
con.Execute(sql2);
}


The issue is the following...
rs.RecordCount doesn't return a valid value - so I use the EOF property and it works fine.

 
good, but i guess this code is not for a project right? u r just playing about with JS arent u?

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top