Skip to Content

DataTable.Rows.Remove kills SAP

I am using SAP 9.2 PL03, C# UI and DI API and trying to delete rows from a Grid's DataTable, but every time I call

Grid oGrid = (Grid)oForm.Items.Item(...ItemID).Specific;
oGrid.DataTable.Rows.Remove(2); // Index does not matter, tried 1 too

SAP just stops working (placed a breakpoint just before the call, the it works until I do call it, and I made sure I have existing rows in the DataTable --> I added 10 rows and tried to delete the first and second).

I tried doing oForm.Freeze(true) before the deletion, but without success, and I tried it during and ItemPress and a Form Open event too. The executing Thread right before the call is MTA (Console.WriteLine( System.Threading.Thread.CurrentThread.GetApartmentState() ;)

Edit:

I could catch the exception:

The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

Edit2:

I explicitly ran the code on STA:

Thread thread = new Thread(RunOnSTA);
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
thread.Join();

private void RunOnSTA()
{
    try
    {
        Grid oGrid = (Grid)oForm.Items.Item(....ItemID).Specific;
        oGrid.DataTable.Rows.Remove(2);
    }  
    catch (Exception e)
    {
        Console.Write("asd");
    }
}

Still crashes the SAP client.

Should I just give up and implement my own function where I get the DataTable as an XML string, Parse/Delete the row and Load it back from XML?

This kind of works, but messes up checkboxes for me:

public static void RemoveRowFromGridUsingXML(Grid oGrid, int rowIndex)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(oGrid.DataTable.SerializeAsXML(BoDataTableXmlSelect.dxs_DataOnly));

    XmlNode node = doc.SelectSingleNode($"/DataTable/Rows/Row[{rowIndex}]");
    if (node != null)
    {
        XmlNode parent = node.ParentNode;
        parent.RemoveChild(node);
        string newXML = doc.OuterXml;
        oGrid.DataTable.LoadSerializedXML(BoDataTableXmlSelect.dxs_DataOnly, newXML);
    }
}
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    May 07 at 01:48 PM

    Okay, It seems like If I do not set the rowheaders (oGrid.RowHeaders.SetValue(i, "indexVal+1")) and try to insert new rows and move around rows at the same time, the next Rows.Delete will fail. (Details in my comments)

    Add comment
    10|10000 characters needed characters exceeded

  • May 02 at 01:58 PM

    Hi Szabolcs Kelemen,

    1. Have you tested the same on the latest available version and patch?

    2. Can this be reproduced on the DEMO Database on the latest available version and patch?

    3. What are the exact steps to reproduce the issue?

    4. Are you removing the row from the Grid once the Grid is populated with the data from the query?

    5. Any other information about this issue?

    Kind regards,

    ANKIT CHAUHAN

    SAP SME Support

    Add comment
    10|10000 characters needed characters exceeded

    • I could somewhat narrow it down, by trying a lot...

      I need to move rows around, so I made a method to push rows down and insert a new row at a certain index. This method seems to cause the issue when I am calling the delete (If I do not call the push method beforehand, the delete method succeeds, but If I call it more than once, SAP will fail on row delete. Note: The push method seems to work fine, the rows are in the expected position and no error is thrown until I call a delete).

      The push method (the commented out portion does the same as the previous longer, both causes fail on delete):

      public void PushDownRowsFromIndex(RowIndex rowIndex)
      {
          Grid oGrid = (Grid)oForm.Items.Item(ItemManagerFormIdentifiers.ID_GRID_MAIN.ItemID).Specific;
          DataTable oDataTable = oGrid.DataTable;
      
          if (oDataTable.Rows.Count == 0)
              return;
      
          var rowDatas = new List<ItemManagerRow>();
          for (int i = 0; i < oDataTable.Rows.Count; i++)
          {
              var rowData = ItemManagerRow.FromGridRow(oGrid, new RowIndex(RowIndexType.ZeroBased, i));
              rowDatas.Add(rowData);
          }
      
          oDataTable.Rows.Clear();
          oDataTable.Rows.Add(rowDatas.Count + 1);
          for (int i = 0; i < rowDatas.Count + 1; i++)
          {
              if (i == rowIndex.IndexAsZeroBased)
              {
                  new ItemManagerRow().WriteToGridRow(oGrid, rowIndex);
              }
              else
              {
                  if (i < rowIndex.IndexAsZeroBased)
                  {
                      rowDatas[i].WriteToGridRow(oGrid, new RowIndex(RowIndexType.ZeroBased, i));
                  }
                  else
                  {
                      rowDatas[i - 1].WriteToGridRow(oGrid, new RowIndex(RowIndexType.ZeroBased, i));
                  }
              }
          }
          /*
          oDataTable.Rows.Add();
      
          for (int i = oDataTable.Rows.Count - 2; i >= rowIndex.IndexAsZeroBased; i--)
          {
              var rowData = ItemManagerRow.FromGridRow(oGrid, new RowIndex(RowIndexType.ZeroBased, i));
              rowData.WriteToGridRow(oGrid, new RowIndex(RowIndexType.ZeroBased, i + 1));
          }
          new ItemManagerRow().WriteToGridRow(oGrid, rowIndex);*/
      }<br>

      The ItemManagerRow object: https://paste.ubuntu.com/p/JHCvJzGNYp/

      Even If I comment out the ItemManagerRow's Write method's body, SAP will freeze on delete.