2
votes

I have a recordset containing 3 records. I am using DO While loop in classic ASP in order to display all the records but the code below only shows the last record in the recordset.

<%Do While Not RS.EOF%>
  <div><%=RS.Fields("ID").value%></div>                 
  <div><%=RS.Fields("Title").value%></div>
  <div><%=RS.Fields("Description").value%></div>
    <%RS.MoveNext()
Loop%>

Can anyone tell me why that is happening? Is it something about the way I fill the recordset?

I know for sure it contains 3 records as RS.RecordCount returns 3.

Here is a code I use to create a dummy recordset and fill it with data:

With RS.Fields
  .Append "ID", adBSTR
  .Append "Title", adBSTR
  .Append "Description", adBSTR
End With
With RS
  .AddNew
  .Fields("ID")="1111"
  .Fields("Title") = "Test1"
  .Fields("Description") = "Test Description 1"
  .Update
End With

With RS
  .AddNew
  .Fields("ID")="2222"
  .Fields("Title") = "Test2"
  .Fields("Description") = "Test Description 2"
  .Update
End With

With RS
  .AddNew
  .Fields("ID")="3333"
  .Fields("Title") = "Test3"
  .Fields("Description") = "Test Description 3"
  .Update
End With
1
Are you properly advancing to the NEXT ROW before populating its cells with the respective data elements? - Bruce David Wilner
the code is posted. Am I? - Coding Duchess

1 Answers

4
votes

From AddNew Method (ADO)

After you call the AddNew method, the new record becomes the current record and remains current after you call the Update method. Since the new record is appended to the Recordset, a call to MoveNext following the Update will move past the end of the Recordset, making EOF True.

This is why it's happened.

When you need to list all of the records of a recordset after adding a new record, you need to be sure that the cursor at the begin.

To do that, call MoveFirst (RS.MoveFirst) before where your loop Do While Not RS.EOF starts.