0

I have the following VB.net code:

Private Function ReorgFileDataset(ByRef InDataSet As DataSet) As DataSet
  Dim _ResultDataRow, _DataRow As DataRow
  Dim _ResultDataSet As New DataSet
  Dim _ResultDataTable As New DataTable
  _ResultDataSet.Tables.Add(_ResultDataTable)
  Dim ColumnName, Value As String
  Dim FileID As Integer
  For Each _DataRow In InDataSet.Tables(0).Rows
     ColumnName = _DataRow.Item("FKP_KEYWORD")
     If Not _ResultDataTable.Columns.Contains(ColumnName) Then
        _ResultDataTable.Columns.Add(ColumnName)
     End If
  Next
  _ResultDataTable.Columns.Add("DATE_SENT")

  _ResultDataRow = Nothing
  For Each _DataRow In InDataSet.Tables(0).Rows
     If FileID <> _DataRow.Item("FD_RID") Then
        If Not _ResultDataRow Is Nothing Then
           _ResultDataTable.Rows.Add(_ResultDataRow)
        End If
        FileID = _DataRow.Item("FD_RID")
        _ResultDataRow = _ResultDataTable.NewRow()
        _ResultDataRow.Item("DATE_SENT") = _DataRow.Item("FD_LASTMODIFIED")
     End If
     If _DataRow.Item("FKP_KEYWORD").GetType IsNot GetType(DBNull) And _
        _DataRow.Item("FKP_VALUE").GetType IsNot GetType(DBNull) Then
        ColumnName = _DataRow.Item("FKP_KEYWORD")
        Value = _DataRow.Item("FKP_VALUE")
        _ResultDataRow.Item(ColumnName) = Value
     End If
  Next
  If _ResultDataRow IsNot Nothing Then
     _ResultDataTable.Rows.Add(_ResultDataRow)
  End If

  Return _ResultDataSet
End Function

When I run it through a converter, I get this C# code:

private DataSet ReorgFileDataset(DataSet InDataSet)
{
    DataRow _ResultDataRow = default(DataRow);
    DataSet _ResultDataSet = new DataSet();
    DataTable _ResultDataTable = new DataTable();
    _ResultDataSet.Tables.Add(_ResultDataTable);
    string ColumnName = null;
    string Value = null;
    int FileID = 0;


    foreach (DataRow _DataRow in InDataSet.Tables[0].Rows) {
        ColumnName = _DataRow.Item("FKP_KEYWORD");
        if (!_ResultDataTable.Columns.Contains(ColumnName)) {
            _ResultDataTable.Columns.Add(ColumnName);
        }
    }
    _ResultDataTable.Columns.Add("DATE_SENT");

    _ResultDataRow = null;
    foreach (DataRow _DataRow in InDataSet.Tables[0].Rows) {
        if (FileID != _DataRow.Item("FD_RID")) {
            if ((_ResultDataRow != null)) {
                _ResultDataTable.Rows.Add(_ResultDataRow);
            }
            FileID = _DataRow.Item("FD_RID");
            _ResultDataRow = _ResultDataTable.NewRow();
            _ResultDataRow.Item("DATE_SENT") = _DataRow.Item("FD_LASTMODIFIED");
        }
        if (!object.ReferenceEquals(_DataRow.Item("FKP_KEYWORD").GetType, typeof(DBNull)) & !object.ReferenceEquals(_DataRow.Item("FKP_VALUE").GetType, typeof(DBNull))) {
            ColumnName = _DataRow.Item("FKP_KEYWORD");
            Value = _DataRow.Item("FKP_VALUE");
            _ResultDataRow.Item(ColumnName) = Value;
        }
    }
    if (_ResultDataRow != null) {
        _ResultDataTable.Rows.Add(_ResultDataRow);
    }

    return _ResultDataSet;
}

I modified the InDataSet.Tables(0) to InDataSet.Tables[0] however, there are several errors that I cannot fix because intellisence doesn't have an Item property for DataRow. I'm also not sure about the last if statement where it is checking for Nulls.

Can I get some help here?

MatthewD
  • 6,719
  • 5
  • 22
  • 41
MB34
  • 4,210
  • 12
  • 59
  • 110
  • What errors are you getting? `DataRow.Item` is a valid property in C# just as it is in VB.NET. The last line is equivalent to the last line in the VB.NET code - if the `DataRow` is not null, it's added to the result. `null` is to C# what `Nothing` is to VB.NET. – Tim Oct 14 '15 at 18:16
  • 1
    Item indexers are implicit on C# types, so in VB if you used `_DataRow.Item()`, in C# it would be `_DataRow[]`. Its a difficult thing for converters to pick up because you can have a C# property or method named `Item`, so it takes the safe road typically and puts it in there forcing you to fix them. – Ron Beyer Oct 14 '15 at 18:16
  • As I stated, NO Item property in the DataRow is shown in Intellisense, ONLY ItemArray. – MB34 Oct 14 '15 at 19:08
  • C# changes the `Item(string)` or `Item(int)` into `[string]` or `[int]`, it will not be shown in Intellisense because it doesn't exist. Its shown on MSDN because its there in VB. You need to change the `_DataRow.Item("")` calls to `_DataRow[""]`. – Ron Beyer Oct 14 '15 at 19:40

1 Answers1

0

This SO post here is pretty much a duplicate...

In C#: Why no 'Item' on System.Data.DataRow?

To be more specific though, you can access columns of a data row using an indexer. For example, you have this line of code...

ColumnName = _DataRow.Item("FKP_KEYWORD");

You can change it to this...

ColumnName = _DataRow["FKP_KEYWORD"].ToString();
Community
  • 1
  • 1
Jim
  • 6,753
  • 12
  • 44
  • 72