End the loop before closing the cursor.