So I'm trying to get all the fields from an IFeature into a dictionary but the IFeature throws an exception when I try to get the OID.

Error HRESULT E_FAIL has been returned from a call to a COM component.

This only happens if I'm using a gdb file, shapefiles work fine. What am I doing wrong?

    public static Dictionary<string, object> ReadFeature(IFeature currentFeature)
        var id = currentFeature.OID;//Exception thrown here
        var featureRow = currentFeature.Table.GetRow(id);
        var fields = featureRow.Fields;

        //Add all the fields and their values to the features list
        var fieldsList = new Dictionary<string, object>();
        for (var i = 0; i < fields.FieldCount; i++)
            fieldsList.Add(fields.Field[i].Name, featureRow.Value[i]);

        return fieldsList;

The IFeature object looks like this at the time of the exception:

IFeature fine

UPDATE: The HasOID property is false on the gdb but on the shapefile it's true. On the shapefile the object id is in the column FID and in the gdb it's OBJECTID. Both are read only and are of type Object ID. Why do they behave differently?

  • I've worked around this problem by simply casting the IFeature to an IRow and that cuts out the middleman of trying to get the OID. However, if in future I require the OID I will no doubt be back to this hurdle. – The Pademelon Sep 19 '16 at 1:01
  • UPDATE: I'm not sure if it's because it's a geodatabase or to do with my work around but all my fields are null. – The Pademelon Sep 19 '16 at 1:50
  • UPDATE: Casting to IRow does not get the values in the attribute table like Table.GetRow() does. – The Pademelon Sep 19 '16 at 2:51

The feature was being loaded without all the data which resulted in the OID not loading. The solution to this problem was to set the IEnumFeatureSetup.AllFields = true;

Reference: GeonetThread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.