When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : Databases, Errors


Question:

Why do I get a closed recordset from my stored procedure? Why don't I get the expected fields from the stored procedure?


[Print this FAQ]

Answer: Let's take a look at the following stored procedure. This procedure needs to get a value from the settingstable to get the record we want.

CREATE PROCEDURE spGetRecord (@param int)
AS

Declare @varID int

SELECT @varID = field2 FROM SettingsTable WHERE ID = @param
SELECT * FROM DataTable where ID = @varID

(of course this could be done with a nested SELECT statement, but this example should give you the general idea)

Now when you try to request a field in the recordset in your ASP page you can get the following error:

"Item cannot be found in the collection corresponding to the requested name or ordinal"

Now most people take another look at their table to verify that the field does exist and start to pull their hairs out because the field really exists in the table.

A similar thing can happen when you use statements like DELETE, INSERT, UPDATE, CREATE, etc. in your stored procedure. You get back a recordset, but it's not the correct one! When you execute the query in the query analyzer it does return the correct values, but your ASP page seems to have incorrect values, or no values at all.

This is because every SQL statement in a stored procedure, by default, sends back a special message informing that it has completed the statement. This special message, among other things, includes the "nn rows affected" message you've probably seen if you've executed SQL statements through SQL Server's Query Analyzer. When ADO gets back these messages, it has trouble differentiating between what SQL statement's values it should populate in the recordset. In the stored procedure in the example the results of the first SELECT @varID= statement are passed to the recordset. The results of the second select (the results we actually want!) are passed as well, as the next recordset, but not as the default recordset. (Of course, you can get to these results using the ADO Recordset's NextRecordSet method - read Using Multiple Recordsets for more information on the NextRecordset method).

Now that we know why this happens, let's see what we can do about it. Essentially, you have to tell SQL server not to return a message to the client for every completed SQL statement in the stored procedure. This can be accomplished via the SET NOCOUNT ON and SET NOCOUNT OFF statements like so:

CREATE PROCEDURE spTest

AS

SET NOCOUNT ON
SELECT @nextID = nextID FROM SettingsTable
SET NOCOUNT OFF

SELECT * FROM DataTable where ID = @nextID

The SET NOCOUNT [ON | OFF] option tells SQL Server to return no information for the statements between the two lines. So always use SET NOCOUNT ON for statements that arenít supposed to return any records!


FAQ posted by Dutch at 11/21/2001 8:17:02 AM to the Databases, Errors category. This FAQ has been viewed 57,541 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article