Is there any difference between assigning a new DataRow value via DataRowExtensions.SetField and through an indexer by name?

  • decompile and have a look :) - tym32167

1 answer 1

Looked at the source and it looks like the same thing as accessing through the indexer:

  public static void SetField<T>(this DataRow row, string columnName, T value) { DataSetUtil.CheckArgumentNull(row, "row"); row[columnName] = (object)value ?? DBNull.Value; } 

With the only difference that Null is replaced with DbNull. However, what is the profit? As far as I remember, nothing prevents you from assigning an ordinary Null.

  • At first it was thought that in some cases null might be a completely correct non-empty value of an object-type column, and they fixed it forever in the indexer. Then they changed their minds, and the SetField extension appeared :-) - Pavel Mayorov
  • Ie correctly assign all the same DbNull, not Null, where not values? - iluxa1810
  • When using an indexer, yes, correctly assign DbNull. Attempting to assign a null may lead to an exception if it is not an object column. - Pavel Mayorov