列不属于DataRow异常

时间:2014-08-09 16:57:51

标签: vb.net

我正在尝试向空数据表添加一行数据。我从另一个数据表中获取该行的数据。

这是我的代码,其中包含我正在尝试做的评论

'create a new blank row with datatable schema 
Dim newRow As DataRow = parsedDataset.Tables("Detail").NewRow

'get data from other datatable (left out the connection details)
topDS = DAL.ExecInLineSQLQuery(sSQL, True)

For Each r As DataRow In topDS.Tables(0).Rows
    For Each col As DataColumn In topDS.Tables(0).Columns
        If parsedDataset.Tables("Detail").Columns.Contains(col.ColumnName) Then
            'An exception is being raised at this line
            newRow(col) = r(col).ToString
        End If
    Next
Next

例外情况表明col不属于表Detail,即使它已通过条件。

1 个答案:

答案 0 :(得分:1)

当您使用DataRow(DataColum)语法为DataRow赋值时,会调用内部方法。请参阅http://referencesource.microsoft.com/#System.Data/data/System/Data/DataRow.cs

private void CheckColumn(DataColumn column) {
    if (column == null) {
        throw ExceptionBuilder.ArgumentNull("column");
    }

    if (column.Table != _table) {
        throw ExceptionBuilder.ColumnNotInTheTable(column.ColumnName, _table.TableName);
    }
}

如您所见,检查不允许使用另一个数据表中的列作为行列值的源。

你可以简单地使用

newRow(col.ColumnName) = r(col.ColumnName).ToString
相关问题