Coding Duchess
Coding Duchess

Reputation: 6899

Classic ASP Do while Loop only shows the last record

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

Upvotes: 2

Views: 960

Answers (1)

Kul-Tigin
Kul-Tigin

Reputation: 16950

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.

Upvotes: 4

Related Questions