Solved

Ado Recordset and Record Count Urgent !!!!

Posted on 2000-04-19
9
311 Views
Last Modified: 2006-11-17
Hi
I am trying to execute a stored procedure with one parameter.If i use the recordset.open method i get the record count as 11 and if i use the command object i get the record count as -1

here is the code for the command object

 Set oConn = New ADODB.Connection
strConnstring = "DSN=Bull;UID=ram;PWD=rajesh"
oConn.Open strConnstring, ram, rajesh, -1

'Create the ADO Database Connection:
Set oComm = New ADODB.Command
Set oPr = New ADODB.Parameter

Set oComm.ActiveConnection = oConn
oComm.CommandText = "ccc_statementofwork"
oComm.CommandType = adCmdStoredProc
oComm.CommandTimeout = 30000
oPr.Type = adInteger
oPr.Value = 134325
oComm.Parameters.Append oPr
Set recBull = New ADODB.Recordset
recBull.CursorLocation = adUseServer
recBull.CursorType = adOpenDynamic
recBull.LockType = adLockBatchOptimistic
Set recBull = oComm.Execute()

The same stored procedure when used in an asp page using the recordset.open method gives the record count as -1

I really need this quite urgently

Any help will be really appreciated

Thanks
0
Comment
Question by:sramkris
9 Comments
 
LVL 14

Accepted Solution

by:
wsh2 earned 75 total points
ID: 2731823
recBull.CursorType = adOpenDynamic
----------------------------------
From MSDN (Please Note the last paragraph).. <smile>:
----------------------------------
RecordCount Property

Indicates the current number of records in a Recordset object.

Return Value:
Returns a Long value.

Remarks:
Use the RecordCount property to find out how many records are in a Recordset object. The property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. Reading the RecordCount property on a closed Recordset causes an error.

If the Recordset object supports approximate positioning or bookmarks—that is, Supports (adApproxPosition) or Supports (adBookmark), respectively, return True—this value will be the exact number of records in the Recordset regardless of whether it has been fully populated. If the Recordset object does not support approximate positioning, this property may be a significant drain on resources because all records will have to be retrieved and counted to return an accurate RecordCount value.

The cursor type of the Recordset object affects whether the number of records can be determined. 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.


0
 
LVL 14

Expert Comment

by:wsh2
ID: 2731830
Change your CursorType -or- execute a separate SELECT COUNT SQL statement to get what you want.. <smile>.
0
 
LVL 1

Author Comment

by:sramkris
ID: 2731860
My cursor type is adopendynamic.The problem still persist.Is it because i use Server side objects.The asp page where is have the code
Set oConn = Server.CreateObject("ADODB.Connection")
strConnstring = "DSN=Bull;UID=ram;PWD=rajesh"
oConn.Open strConnstring, ram, rajesh, -1
oConn.CommandTimeout = 30000
set rsIni = server.CreateObject ("Adodb.recordset")
      Set rsIni.ActiveConnection = oConn
        rsIni.Open "ccc_statementofwork " & 134325,,adOpenKeyset,adLockBatchOptimistic  
     
gives the same error.If the cursor type is adopenkeyset or adopendynamic the result is the same
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2731895
rsIni.Open "ccc_statementofwork " & 134325,,adOpenKeyset,adLockBatchOptimistic    

"ccc_statementofwork " & 134325
       
Are you trying to pass a parameter here? You have to put Command Parameters into the Parameter collection to do so.. I think your -1 is because you are erroring out.. <smile>.  
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Expert Comment

by:wsh2
ID: 2731913
From MSDN:

---------------------------------------
Parameters Collection

Contains all the Parameter objects of a Command object.

Remarks

A Command object has a Parameters collection made up of Parameter objects.

Using the Refresh method on a Command object's Parameters collection retrieves provider parameter information for the stored procedure or parameterized query specified in the Command object. Some providers do not support stored procedure calls or parameterized queries; calling the Refresh method on the Parameters collection when using such a provider will return an error.

If you have not defined your own Parameter objects and you access the Parameters collection before calling the Refresh method, ADO will automatically call the method and populate the collection for you.

You can minimize calls to the provider to improve performance if you know the properties of the parameters associated with the stored procedure or parameterized query you wish to call. Use the CreateParameter method to create Parameter objects with the appropriate property settings and use the Append method to add them to the Parameters collection. This lets you set and return parameter values without having to call the provider for the parameter information. If you are writing to a provider that does not supply parameter information, you must manually populate the Parameters collection using this method to be able to use parameters at all. Use the Delete method to remove Parameter objects from the Parameters collection if necessary.

---------------------------------------
Parameter Object

A Parameter object represents a parameter or argument associated with a Command object based on a parameterized query or stored procedure.

Remarks

Many providers support parameterized commands. These are commands where the desired action is defined once, but variables (or parameters) are used to alter some details of the command. For example, an SQL SELECT statement could use a parameter to define the matching criteria of a WHERE clause, and another to define the column name for a SORT BY clause.

Parameter objects represent parameters associated with parameterized queries, or the in/out arguments and the return values of stored procedures. Depending on the functionality of the provider, some collections, methods, or properties of a Parameter object may not be available.

With the collections, methods, and properties of a Parameter object, you can do the following:

Set or return the name of a parameter with the Name property.

Set or return the value of a parameter with the Value property.

Set or return parameter characteristics with the Attributes and Direction, Precision, NumericScale, Size, and Type properties.

Pass long binary or character data to a parameter with the AppendChunk method.
If you know the names and properties of the parameters associated with the stored procedure or parameterized query you wish to call, you can use the CreateParameter method to create Parameter objects with the appropriate property settings and use the Append method to add them to the Parameters collection. This lets you set and return parameter values without having to call the Refresh method on the Parameters collection to retrieve the parameter information from the provider, a potentially resource-intensive operation.

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2732130
Dear sramkris, i think you were so close to your solution:
replace
Set recBull = oComm.Execute()
by
recBull.Opne oComm

Your statement erases the recordset you defined beforehand, and the command object always returns a ForwardOnly recordset (including that Recordcount will be -1 unless you did a Movelast, but then you can't go back :-(

hope this helps
0
 

Expert Comment

by:AlessandroF
ID: 2735034
You can try with

oConn.Open strConnstring, ram, 3,3

0
 

Expert Comment

by:MIrfan
ID: 2740175

An ADO recordset is unable to provide a definite value for Recordcount when using dynamic cursors.Try Changeing your cursor type :

recBull.CursorLocation = adUseClient
recBull.CursorType=adOpenKeyset

Having said that, you do not always get the cursor you asked for.If you asked for certain kind of cursor combination ,which cannot be supported by your provider,ADO will give you another cursor combination that is supported by the provider without generating an error message.SO make sure you got what you asked for ,by checking the cursorType property on the Recordet after you have opened the Recordset.When you cursor get a -1 in the Recordcount that shows the cursor does not support Recordcount.SO change it to Keyset cursor instead of Dynamic cursor.This should solve you problem.Wish you Good Luck.
0
 
LVL 1

Author Comment

by:sramkris
ID: 2752542
Thanks a lot guys for helping me out.This was a lot of information i received.

Ram
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now