Forum Discussion

Adagio's avatar
Adagio
Frequent Contributor
6 years ago

Data provider or other service returned an E_FAIL status

Hello,

 

I'm trying to run a piece of code which copies data from a Database table and paste it in a spreadsheet.

But I get the error "Data provider or other service returned an E_FAIL status." at the highlighted line.

 

 

  var dbTable = conn.Execute_(execQuery); 

  var excel = Sys.OleObject("Excel.Application");
  var wb = excel.Workbooks.Open(fileName);
  var ws = wb.Sheets.Item(sheetName);
  excel.DisplayAlerts = false;

  // Copy All field names
  for (var i = 0; i < dbTable.Fields.Count ; i++)
    ws.Cells.Item(1, i + 1).Value = dbTable.Fields.Item(i).Name;
  // Scans all records returned by the query
  Log.Message("Total Fields: " +dbTable.Fields.Count);
  dbTable.MoveFirst();
  var RowIndex = 2;
  while (! dbTable.EOF)
{
    for (i = 0; i < dbTable.Fields.Count; i++)
      ws.Cells.Item(RowIndex, i + 1).Value = dbTable.Fields.Item(i).Value;
dbTable.MoveNext();
RowIndex++;

Any suggestions or help would really be appreciated!

 

Thank you

Abhi

 

4 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi Abhi,

     

    A quick question: does the query return data?

    (If I remember it correctly, in case of absent data, both .EOF and .BOF equal to true)

    • Adagio's avatar
      Adagio
      Frequent Contributor

      Hi Alex,

       

      Yes, the query returns the data( 1 row), but not sure why it fails at MoveFirst()

       

      Thank you

      Abhi

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi Abhi,

         

        Then, most probably, the conn.Execute_(execQuery); line returns forward-only recordset (I used to use plain ADO objects and explicitly specify recordset parameters).

        If this is the case, then you should just omit the call to .MoveFirst().

        Note, that in case of forward-only recordset it is not possible to rewind it and you must close and re-open it if you need another iteration.