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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting A value out of a db 2

Status
Not open for further replies.

mbutler55

Instructor
Feb 5, 2003
33
US
This has to be THE dumbest question ever asked, but I feel like I have tried everything and nothing seems to work! I have a list of numbers, sorted in ascending order in a table called Median - (yes I am trying to find the median value)

My code to peruse the list of numbers until finding the middle number is:
' First I find the total number of records. Is there an
easier way?
iNumRec = 1
Do while NOT objRS.EOF
iNumRec = iNumRec + 1
objRS.MoveNext
Loop
' Now to peruse to the middle record
for iCounter = 1 to iNumRec \ 2
next
' I am now at the middle record what comes next?
response.write _________
 
objRS.RecordCount should give you the total records

The problem with just going to the count / 2 is how do you handle odd # record sets? There was a thread on calculating medians a while back. Someone had an example going. Search for median.
Jessica [ponytails2]
 
I already did a keyword search for Median and couldn't find anything that would help. I do have other code with this that takes into account odd records / even records. My question is simply how to get a value to display from the database. What goes in the blank?

If iCounter is pointing at the value that I want to see on the screen, what do I response.write?
response.write _________
 
iNumRec = 1
Do while NOT objRS.EOF
iNumRec = iNumRec + 1
objRS.MoveNext
Loop
objRS.MoveFirst
' Now to peruse to the middle record
for iCounter = 1 to iNumRec \ 2
objRS.MoveNext
next
' I am now at the middle record what comes next?
response.write objRS("DBaseFieldName")

Is this what you mean?

BDC.
 
With the code you have there it you would have to re-enter the recordset to retrieve the value. Probably best to do as Jessica suggested and use RecordCount.

Make sure you open your recordset with an 'adOpenStatic' cursorType. This will allow you to use RecordCount. You will get a value of -1 if you open the recordset using the default cursorType.

Something like this might be OK for you.
Code:
<%
intCount = 0
intMedian = CInt(objRS.RecordCount / 2)

While NOT objRS.EOF
  intCount = intCount + 1

  If intCount = intMedian Then
    Response.Write objRS(&quot;YourFieldName&quot;)
    objRS.MoveLast
  End If

  objRS.MoveNext
Wend
%>
Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
If this works, I will call you both the &quot;Greatest Programmer that ever lived!!!!&quot; But, how about one more. And let's see how can type the fastest:

If I am wanting the user to type in Username and Password on page1, click the submit button and then on page2 filter the data to see if the user exists. I know that I will perform a check for EOF, but how do I create the filter or whatever.

Dim strName, strPassword
strName = Request (&quot;Username&quot;)
strPassword = Request (&quot;Password&quot;)
Session (&quot;Username&quot;) = strName
Session (&quot;Password&quot;) = strPassword

Is it something like:
objRS.Filter = &quot;Invest = &quot; & strName & &quot;&Password = &quot; & strPassword&quot;

and then check for EOF?
 
Create the filter in the DB call

&quot;SELECT * FROM Tablename WHERE name = &quot;whatever&quot; AND password = &quot;whatever&quot;

If the recordset contains no records (rs.EOF) then the user doesn't exist.

BDC.
 
I would use the SQL to check the DB.
Code:
<%
strSQL = &quot;SELECT * FROM tblUsers WHERE Invest='&quot; & strName & &quot;' AND Password='&quot; & strPassword & &quot;';&quot;

Set objUsersRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
objUsersRS.Open strSQL, strYourConnectionString

If objUsersRS.EOF Then
 'no match found
Else
 'user found and verified
End If

objUsersRS.Close
Set objUsersRS = Nothing
%>
Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
I bow down to you both!!! That is why they call you &quot;Programmer&quot; and me &quot;Instructor&quot; - those who can do, those who can't pretend to know what they are talking about in a class where no one has a clue!
 
I really do hate this stuff! Give me back my Pascal from 1986! It was so easy to read!

What is causing the following error:
Invalid character
intCount = intCount + 1

What can possibly be wrong with x = x + 1?
 
First of all if this is not your code, please don't think I am showing preference. I did try them both this is just the last one that I have! This is not the entire page of code just the snippet where the problem seems to occur. If I take this code out all is well.

'Count the number of Age Median
Dim tempAge, x, iNumRec, Median, intMedian
Set objRS = Server.CreateObject (&quot;ADODB.Recordset&quot;)
sql=&quot;SELECT PTAGE FROM Median&quot;
objRS.Open sql, objConn

Response.Write (&quot;Age Median: &quot;)
objRS.MoveFirst
x = 0
intMedian = CInt(objRS.RecordCount / 2)
While NOT objRS.EOF
  x = x + 1
  If x = intMedian Then
   Response.Write objRS(&quot;PTAGE&quot;)
   objRS.MoveLast
  End
If
   objRS.MoveNext
Wend

PS - I REALLY do appreciate you and yours taking the time to look over this with me. Both of you have been VERY easy to understand. You are not talking in code!
 
I should add that in order to use the ADO constant, adOpenStatic, you need to have included the following line at the top of your page (or the top of any of your other include files)

<!-- METADATA TYPE=&quot;typelib&quot; FILE=&quot;C:\Program Files\Common Files\System\ado\msado15.dll&quot; -->

This file contains all the the ADO constants that are used in ASP scripts. You can always use the value 3 instead of adOpenStatic if you havent included the above file.

Hope that made sense :) Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
Your moving to the last record in your loop then trying to move to the next record in the following statement, try this

While NOT objRS.EOF
x = x + 1
If x = intMedian Then
Response.Write objRS(&quot;PTAGE&quot;)
objRS.MoveLast
Else
objRS.MoveNext
End If
Wend

BDC.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top