Click to See Complete Forum and Search --> : Simple Value Passing


st3ady
06-16-2006, 01:48 PM
Ok I have an easy question :D

I have a page that generates a table and fills it with data from an Access database, and while it populates the table I have a counter to count each record that is added.
At the bottom of the page I have a display that says "Records found: "
and then a <% Response.Write J%> and I was wondering, how can I get that line to show up at the top of the page before the table is generated? I tried it but since J is empty until after the table is made, it doesn't work. I want to pass the value up to the top... :confused: Should I use a javascript function? Thanksies

dirt29
06-16-2006, 02:56 PM
Heya,

I'm pretty new at this also, but I think you code do something like so:



<%@ Language=VBScript %>

<%


Option Explicit

'variables
Dim objDBConn
Dim sSQL
Dim oRS
Dim sDSN
dim nRecordCount

'connect to db
set objDBConn = server.createobject ("adodb.connection")
sDSN = "Driver={SQL Server};Server=media;Database=xxx;Uid=xxx;Pwd=xxx"
Call objDBConn.open (sDsn)

'query to get total
sSQL = "select count(*) from YOURTABLE"
'open recordset
Set oRS = objdbconn.execute (sSQL)
If Not oRS.EOF Then
'get value
nRecordCount = cLng("0" & ors(0))
End If
'tidy up
Call oRS.Close
Set oRS = Nothing

%>


<!-- to display the record count -->


<html>
<body>
Record count is <%= nRecordCount %>
</body>
</html>



The only thing you will probably need to modify is the connection string, depending on what DB your trying to access.

russell
06-16-2006, 03:55 PM
even better owuld be to use the ADO GetRows method which pops the recordset in an array, then loop through the array writing the data out, instead of looping through the recordset. Then, at the top of your page you can write out the Ubound() of the array

This Thread (http://www.webdeveloper.com/forum/showthread.php?t=109879) demonstrates exactly how to do that.

Only modification would be that right b4 this line
.Write "<table>" & vbCrLf

You'd add
Response.Write ubound(ar, 2) + 1 & " Records"

cwilson
06-16-2006, 04:31 PM
Another way to do this is to create your html table at the top of your page and save the whole table as a really long string, and the number of records as a variable. Then where you want to display the table, just display the string

<%= strTable %>

This is probably a better solution even if you don't need a pagecount at the top because it helps separate your asp code from your html code.

lmf232s
06-16-2006, 04:38 PM
how about

objRS.RecordCount

http://www.asp101.com/samples/viewasp.asp?file=db%5Fcount%2Easp

cwilson
06-16-2006, 04:45 PM
Depending on the cursor type/locking type (I can't remember which) RS.RecordCount may not work. If it works, then definately use that, but if it doesn't then you'll have to use your handmade counter.

Of course, there are other options. You can do this in JS too, using ASP to count the number of records and dropping that number inside JS code.

st3ady
06-16-2006, 05:12 PM
thanks for the replies guy. Yeah, I did not make it clear that I am not just counting all the records in the database. I have a page setup so that the user can choose the date range and also filter it. So I have a counter count up the amount of records they chose to display and then use that variable to do a conversion factor calculation. I have that working fine, just want it to display up top under the title of the table. If I go the Javascript route, I would pass the variable, and then what? Maybe I should head over to the javascript forum?

russell
06-16-2006, 09:28 PM
you could, but what you really should do is pass those dates to the query, not filter the recordset client-side. and the most efficient way to write tabular data from a recordset is to use the ADO GetRows method and loop the array, not the recordset. 2 reasons -- (1) u can close the rs right away, thus freeing server resources (and the most expensive ones at that) and (2) IIS loops through arrays about 4 to 5 times faster than walking thru recordsets. Then ubound(ar, 2) + 1 will tell you how many records BEFORE you start writing code to display the data.

if u really want to use js, there is nothing stoping you. u really ought to check out that link i posted though...

if you want to use JS, then create a hidden DIV or SPAN, then when you know the # of records by iterating your recordset, all u have to do is update the innerHTML property of the div/span and then unhide it with the onload event handler of the <body> tag.