SQL Count

I have this Page:

<%
'Declare and Set our variables
Dim strSQL, objRs, objConnectionection, strConnectString
Dim intMaxSize, intCurrentSize, intM
 
Session("varClassName")      =      CheckInput(Request("ClassName"))
Session("varClassDate")      =      CheckInput(Request("ClassDate"))
intM           =     CInt(Request("M"))
intCurrentSize     =     0
strConnectString = "DRIVER={SQL Server};SERVER=appalachian\intrasql1;DATABASE=CSSD;UID=cssdUser;PWD=cs#sd4$xg"

Response.write("<p>The new class Name is: " & Session("varClassName") & "<P>")
Response.write("<p>The new class date is: " & Session("varClassDate") & "<P>")
Response.write("<p>The new class size is: " & intM & "<P>")

' Get Class information and Count while taking advantage of the connection object
' Syntax may change due to the type of database you are using. Such as MS Access
' needs the date to have # at the beginning and ending:  #" & Date & "#
' MS SQL Uses ' so it should be '" & Date & "'

strSQL = "SELECT * FROM RegCollect Where ClassName = '" & Session("varClassName") & "' " & _
               "And ClassDate = '" & Session("varClassDate") & "'"

' open your connection to the server. Depending on the database


set objConnection=Server.CreateObject("ADODB.Connection")
objConnection.ConnectionTimeout = 15
objConnection.CommandTimeout =  10
objConnection.Mode = 3 'adModeReadWrite
objConnection.Open strConnectString


'Create Recordset and get count
' intCurrentSize will hold affected records or in this case the count
Set objRs = objConnection.Execute(strSQL, intCurrentSize)

If Not objRs.eof Then
    Response.Write("<Table Border=1 cellpadding=2 cellspacing=2>")
    do while not objRS.EOF
       Response.Write("<TR>")
       for j=0 to objRS.Fields.count-1
            Response.Write("<TD>" & objRS(j).Value & "</TD>")
       next
       Response.Write("</TR>")
       objRS.MoveNext
    loop
    Response.Write("</Table>")
Else
    Response.write("No records found!<p>")
End If

    If intM <= intCurrentSize then
          Response.write("Class is full!")
     Else
         'Response.Redirect("default.asp")
     End IF

Response.write("<p>the Current Size is: " & intCurrentSize & "<P>")
'If Not objRs.eof Then
     ' Do things in here with the data, for only one record
     ' Use Do While not objRs.EOF ..... Loop for All returned records
     ' If you loop through, move this next If statement out of the loop
    ' If intM <= intCurrentSize then
          'Class is at max
    ' Else
          'There is room
    ' End IF
'Else
     ' Not records exists for this search, give the user an error message
'End If

objRs.Close
Set objRs = Nothing
objConnection.close
set objConnection = Nothing



'This should make things SQL friendly
Function CheckInput(n)
     If n <> "" Then
          If Instr(n,"'")<>0 Then n = Replace(n,"'","''")
          If Instr(n,"""")<>0 Then n = Replace(n,"""","""""")
          CheckInput=Trim(n)
     End If
End Function
%>

It is supposed to limit records being added to a table once a limit is reached

M is the class size alowed and the value being passed is 4

intCurrentSize is supposed to supply the current amount of records.  it is not working, here is the output I am getting in html.


The curent size comes up as -1 when it should be 2.

OK Experts, how do I fix this.
LVL 2
dtoloAsked:
Who is Participating?
 
Brendt HessConnect With a Mentor Senior DBACommented:
Once you have the open recordset, then

intCurrentSize = objRS.RecordCount

in your code.

----------------------------------------

Now that we have discussed this, you would be better off using a stored procedure to return this recordset.  You can have the SProc return the RecordCount in the Return Value; the recordset would still come in as normal.
0
 
dtoloAuthor Commented:
For more info this question is continued from a previous post:  http://experts-exchange.com/Web/Web_Languages/ASP/Q_21172297.html
0
 
rdrunnerCommented:
-1 means the script cannot tell you how many records the querry returned.

Try to change this querry to a client side cursor. This will allow you to get the correct count. Another (better) option would be to querry the DB for the real count and use that value (Select count(*) myCount from myTable where .....) this will only return one row you need to querry to get the result and it wont return the  4 rows of data which you dont really need here.

Hope this makes sense
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
dtoloAuthor Commented:
ok,

I rem'd and un rem'd the response redirect and added a few more records to get it over 4

I get:

The new class Name is: collect training


The new class date is: 10/5/04 6:00 am


The new class size is: 4

30 111111  collect training 10/5/2004 6:00:00 AM
32 396893 JDT8482 collect training 10/5/2004 6:00:00 AM
39 271383 0 collect training 10/5/2004 6:00:00 AM
40 600701 JD324356 collect training 10/5/2004 6:00:00 AM
41 633069 0 collect training 10/5/2004 6:00:00 AM


the Current Size is: -1

The html output:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>New Page 1</title>
</head>

<body>
<p>The new class Name is: collect training<P><p>The new class date is: 10/5/04 6:00 am<P><p>The new class size is: 4<P><Table Border=1 cellpadding=2 cellspacing=2><TR><TD>30</TD><TD>111111</TD><TD></TD><TD>collect training</TD><TD>10/5/2004 6:00:00 AM</TD></TR><TR><TD>32</TD><TD>396893</TD><TD>JDT8482</TD><TD>collect training</TD><TD>10/5/2004 6:00:00 AM</TD></TR><TR><TD>39</TD><TD>271383</TD><TD>0</TD><TD>collect training</TD><TD>10/5/2004 6:00:00 AM</TD></TR><TR><TD>40</TD><TD>600701</TD><TD>JD324356</TD><TD>collect training</TD><TD>10/5/2004 6:00:00 AM</TD></TR><TR><TD>41</TD><TD>633069</TD><TD>0</TD><TD>collect training</TD><TD>10/5/2004 6:00:00 AM</TD></TR></Table><p>the Current Size is: -1<P>

</body>

</html>
0
 
dtoloAuthor Commented:
What would the syntax be for your second option.  How does the variable get the count (that is really confuseing me)

I only show the four rows to see if it is working (debug).  I will get rid of it once the code is all set.
0
 
dtoloAuthor Commented:
How do I store the count in a variable?
0
 
NavicertsCommented:
VarthatHoldsCount = objConnection.Execute(strSQL, intCurrentSize)


strSQL = "SELECT Count(*) FROM someplace"

-Navicerts
0
 
dtoloAuthor Commented:
Well I tried doing it the way you guys suggested and I wasn't getting any where.  

I ended up jury rigging it and it works.  I did this:

<%
'Declare and Set our variables
Dim strSQL, objRs, objConnectionection, strConnectString
Dim intMaxSize, intCurrentSize, intM
 
Session("varClassName")      =      CheckInput(Request("ClassName"))
Session("varClassDate")      =      CheckInput(Request("ClassDate"))
intM           =     CInt(Request("M"))
intCurrentSize     =     0
strConnectString = "DRIVER={SQL Server};SERVER=appalachian\intrasql1;DATABASE=CSSD;UID=cssdUser;PWD=cs#sd4$xg"

Response.write("<p>The new class Name is: " & Session("varClassName") & "<P>")
Response.write("<p>The new class date is: " & Session("varClassDate") & "<P>")
Response.write("<p>The new class size is: " & intM & "<P>")

' Get Class information and Count while taking advantage of the connection object
' Syntax may change due to the type of database you are using. Such as MS Access
' needs the date to have # at the beginning and ending:  #" & Date & "#
' MS SQL Uses ' so it should be '" & Date & "'

strSQL = "SELECT * FROM RegCollect Where ClassName = '" & Session("varClassName") & "' " & _
               "And ClassDate = '" & Session("varClassDate") & "'"

' open your connection to the server. Depending on the database


set objConnection=Server.CreateObject("ADODB.Connection")
objConnection.ConnectionTimeout = 15
objConnection.CommandTimeout =  10
objConnection.Mode = 3 'adModeReadWrite
objConnection.Open strConnectString


'Create Recordset and get count
' intCurrentSize will hold affected records or in this case the count
Set objRs = objConnection.Execute(strSQL)

If Not objRs.eof Then
    Response.Write("<Table Border=1 cellpadding=2 cellspacing=2>")
    do while not objRS.EOF
       Response.Write("<TR>")
       for j=0 to objRS.Fields.count-1
            Response.Write("<TD>" & objRS(j).Value & "</TD>")
           
       next
       Response.Write("</TR>")
       objRS.MoveNext
       intCurrentSize = intCurrentSize + 1
    loop
    Response.Write("</Table>")
Else
    Response.write("No records found!<p>")
End If

    If intM <= intCurrentSize then
          Response.write("Class is full!")
     Else
         'Response.Redirect("default.asp")
     End IF

Response.write("<p>the Current Size is: " & intCurrentSize & "<P>")
'Response.write("<p>the count is: " & & "<P>")
'If Not objRs.eof Then
     ' Do things in here with the data, for only one record
     ' Use Do While not objRs.EOF ..... Loop for All returned records
     ' If you loop through, move this next If statement out of the loop
    ' If intM <= intCurrentSize then
          'Class is at max
    ' Else
          'There is room
    ' End IF
'Else
     ' Not records exists for this search, give the user an error message
'End If

objRs.Close
Set objRs = Nothing
objConnection.close
set objConnection = Nothing



'This should make things SQL friendly
Function CheckInput(n)
     If n <> "" Then
          If Instr(n,"'")<>0 Then n = Replace(n,"'","''")
          If Instr(n,"""")<>0 Then n = Replace(n,"""","""""")
          CheckInput=Trim(n)
     End If
End Function
%>

If any one tells me a better way and why, I will award points.  Otherwise I will close this question in a couple of days.
0
 
Brendt HessSenior DBACommented:
What is happening here is a classic Forward-Only, Read-only cursor issue.  As noted in the ADO documentation:

"The RecordCount property will return -1 for a forward-only cursor, the actual count for a static or keyset cursor, and either -1 or the actual count, depending on the data source, for a dynamic cursor."

Since the .Recordcount property can be accessed at any time, if the service is using a cursor of this type, then the .Recordcount will return 0 (none) or -1 (have some unspecified amount).  However, a recordset can be set to be static, client-side, read-only.  In this case, the .Recordcount will be the correct value.

To do that in this case, change:

---------------------------------------
'Create Recordset and get count
' intCurrentSize will hold affected records or in this case the count
Set objRs = objConnection.Execute(strSQL)
---------------------------------------

to

---------------------------------------
'Create Recordset and get count
' intCurrentSize will hold affected records or in this case the count
Set objRs = new adodb.Recordset
objRs.CursorLocation = adUseClient       ' adUseClient Value = 3
objRS.Open strSQL, objConnection, adOpenStatic, adLockReadOnly, adCmdText  
' Constant values:  adOpenStatic = 3
'                           adLockReadOnly = 1
'                           adCmdText = 1

Set objRS.ActiveConnection = Nothing
-----------------------------------------

Your recordset will now have a correct .Recordcount property.
0
 
dtoloAuthor Commented:
I still don't understand how intCurrentSize will end up getting the value with your method.
0
 
dtoloAuthor Commented:
Thank you bhess,

I continued this question to:  http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21176934.html

I would like more info on using a sproc.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.