0

I am populating an ADOdb recordset to Excel using C# interop.

while (!rs.EOF)
  {
    Excel.Worksheet xlMainWs = xlList.Find(x => x.Name.Equals("tMain"));
    ((Excel.Worksheet)xlMainWs).Select();
    Excel.Range mainc1 = xlMainWs.Cells[refStartRow + 1, refStartCol];
    Excel.Range mainc2 = xlMainWs.Cells[refStartRow + 1, refEndCol];
    Excel.Range mainRng = (Excel.Range)xlMainWs.get_Range(mainc1, mainc2);
    mainRng.ClearContents();
    mainRng.CopyFromRecordset(rs, 1);
    rs.MoveNext();
  }

While I can populate the recordset to the excel range, the problem is it keeps populating the first row from the recordset. I tried putting:

rs.MoveNext();

but it's still populating the first row of the recordset.

I did the same in Excel VBA, the loop will move the record cursor to next record.

Why doesn't this work the same in C#?

3
  • Where did you put rs.MoveNext();? Jan 16, 2020 at 5:15
  • [code] mainRng.CopyFromRecordset(rs, 1); rs.MoveNext(); right here
    – Colin Chin
    Jan 16, 2020 at 5:26
  • That's important, so edit the question to show it there. That way viewers can see that it's in the right place and they can look elsewhere for explanations. Jan 16, 2020 at 5:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.