Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Count

Posted on 2004-10-20
11
Medium Priority
?
779 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:dtolo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 2

Author Comment

by:dtolo
ID: 12359210
For more info this question is continued from a previous post:  http://experts-exchange.com/Web/Web_Languages/ASP/Q_21172297.html
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 12359313
-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
 
LVL 2

Author Comment

by:dtolo
ID: 12359327
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 2

Author Comment

by:dtolo
ID: 12359380
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
 
LVL 2

Author Comment

by:dtolo
ID: 12359499
How do I store the count in a variable?
0
 
LVL 7

Expert Comment

by:Navicerts
ID: 12359734
VarthatHoldsCount = objConnection.Execute(strSQL, intCurrentSize)


strSQL = "SELECT Count(*) FROM someplace"

-Navicerts
0
 
LVL 2

Author Comment

by:dtolo
ID: 12359984
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
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 12360482
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
 
LVL 2

Author Comment

by:dtolo
ID: 12360800
I still don't understand how intCurrentSize will end up getting the value with your method.
0
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 2000 total points
ID: 12362018
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
 
LVL 2

Author Comment

by:dtolo
ID: 12368450
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question