James
James

Reputation: 1572

ASP ADO recordset.NextRecordset() breaks when there is no next recordset

I'm writing a script in ASP with Javascript, and I need to loop through an indeterminate number of records AND recordsets. I have no problem looping through the individual records. The problem arises when I need to move through recordsets. As long as there is another recordset it works fine. If there are no more recordsets the code dies, and there seems to be no way to check for or catch the error. I've done try/catch blocks and every test I can think of, but the code just breaks and gives internal server error. I was hoping there was an isNextRecordset() or isLastRecordset() function I was missing.

My Current Code

do{
    //Some Code Stuff

    record = record.NextRecordset();  //Dies here when no more recordsets

}while(!!record);

With normal VBScript they show this method working(never tried it though). But I don't want to use VBScript.

VBScript Code

Do Until record = Nothing

   set record = record.NextRecordset
Loop

EDIT: I believe I have made things confusing by adding the loop, If you remove the loop and just call nextrecordset once when there is no next recordset it still dies. No test for null needed. My code doesn't make it to the test for null.

//Get A recordset
//There is only one recordset
record = cmd.Execute();

//Do some stuff with the record set

//Call NextRecordset
//even though there are no more
record = record.NextRecordset();  //<--- Still Dies right here 

EDIT: Just wanted to point out the hidden problem I was having. The answer below is correct but wasn't my actual problem. Some of the tutorials I saw show a structure like below. Close the record and connection. But by the time the record got the bottom it was already null, which was a problem. This is easily solved with correct error messages showing, which I didn't have. My own stupidity, but might help someone.

try{

   //Do Some Stuff

   record = record.NextRecordset();

   //Test Record set  

}catch(e){

}finally{
    record.Close();  <---- This is where it was actually having a problem. 
    record = null;
    myconn.Close();
    myconn = null;
}

Upvotes: 0

Views: 1477

Answers (1)

Shadow Wizard
Shadow Wizard

Reputation: 66389

Proper syntax would be:

while (record) {
    //Some Code Stuff

    record = record.NextRecordset();
}

This way the loop would stop when NextRecordset() will return null.

Upvotes: 1

Related Questions