How do I obtain a NULL value from an ArcGIS.Core.Data.Row?

260
3
04-08-2020 05:00 AM
GernotNeppert
New Contributor

How do I obtain a NULL value from an ArcGIS.Core.Data.Row?

I have a nullable field "Timestamp" in the Database.

When I iterate using a RowCursor, I can find no way to obtain that NULL value using the ArcGIS Pro SDK for DotNet.

I get a valid Row object and I can access all the other fields using the Indexer, just not the NULL field!

Something like this:

while(cursor.MoveNext()) {     
  Row row = cursor.Current;     
  DateTime timestamp  = (DateTime)row["Timestamp"];  //Works only if not NULL. Throws NullReferenceException otherwise
}
Tags (3)
0 Kudos
3 Replies
GintautasKmieliauskas
Regular Contributor

Hi Gernot,

You can assign row value to object type variable. Then check if it is null. If not - assing it to timestamp variable as in your code.

Another way is to use Convert.ToDateTime like this:

DateTime timestamp  = Convert.ToDateTime(row["Timestamp"]);

0 Kudos
GernotNeppert
New Contributor

Hi Gintautas,

thanks very much for your quick reply. That solved the problem for me!

I have to admit I didn't take into account that, in DotNet, a cast from null to another type can actually fail - if that type is a struct!

I guess that programming Java for 20+ years has spoiled me somehow

Regards,

Gernot

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I used the following snippet to check if a column value of a Row is null:

  foreach (var field in fields)
  {
    var val = row[field.Name];
    if (val is DBNull || val == null) continue;
    ...
  }

the code is used in this sample:  https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/2d6a30e2597c7a0079ae65b182f60a77e79aae...

The sample has to ignore null columns.  

0 Kudos