[GIS] Zooming to first queried feature using ArcObjects

arcgis-10.0arcobjectsc

I'm attempting to query a feature class for all features that meet a certain criterion (in this case, the Status field is NULL). However, I'm not getting very far into the attempted method before ArcMap crashes completely. I have no reasonable idea on how to trace errors that cause a program crash, for starters, so I'm more confused than necessary, I expect. I'm building this in VS 2008 Express, for what it's worth.

 public void PerformAttributeQuery(ESRI.ArcGIS.Geodatabase.IFeatureClass fc)
    {
        ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
        queryFilter.WhereClause = "[Status] IS NULL"; // create the where clause statement 

Everything is fine for that first couple of statements, but this query causes a crash:

        // query the table passed into the function and use a cursor to hold the results
        ESRI.ArcGIS.Geodatabase.IFeatureCursor featurecursor = fc.Search(queryFilter, false);

It crashes before reaching the next line (I put in debug message boxes for testing), but I'll put it here for propriety:

        ESRI.ArcGIS.Geodatabase.IFeature feature = (ESRI.ArcGIS.Geodatabase.IFeature)featurecursor.NextFeature();
        ESRI.ArcGIS.ArcMapUI.IContentsView currentContentsView = ArcMap.Document.CurrentContentsView as IContentsView;
        ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)currentContentsView.SelectedItem;
        ESRI.ArcGIS.Carto.IFeatureSelection fSelection = featureLayer as ESRI.ArcGIS.Carto.IFeatureSelection;
        fSelection.Add(feature);
        FindCommandAndExecute(ArcMap.Application as ESRI.ArcGIS.Framework.IApplication, "{AB073B49-DE5E-11D1-AA80-00C04FA37860}");
    }

I cannibalized the PerformAttributeQuery snippet to work with FeatureClasses instead of Tables (or so I had hoped), if you'd like a proper reference. My questions are:

  1. How can I get better reporting of crash errors?
  2. Where did I go wrong? Answer: I must shamefacedly admit that I used the wrong characters to denote the location of the feature table I was querying. Changing the [Status] in my queryFilter.WhereClause to \"Status\" quite literally solved all obvious problems.

Best Answer

see code below

Note that sometimes useful exception messages can be embedded inside of a fairly cryptic general message, so it is important to examine InnerExceptions.

protected override void OnClick()
{
    string path = @"C:\projects\NetTools\InfrastructureEditingTemplate\MapsandGeodatabase\LocalGovernment.gdb";
    try
    {
        var ws = Open(path);
        var fc = ((IFeatureWorkspace)ws).OpenFeatureClass("wMain");
        IFeature feat = QueryFirstFeature(fc);
        if (feat != null)
        {
            //todo: project if necessary
            ((IActiveView)ArcMap.Document.FocusMap).Extent = feat.Shape.Envelope;
            ((IActiveView)ArcMap.Document.FocusMap).Refresh();
        }
    }
    catch (Exception ex)
    {
        while (ex != null)
        {
            Debug.Print("{0} \n {1}", ex.Message, ex.StackTrace);
            ex = ex.InnerException;
        }
    }
}

private IFeature QueryFirstFeature(IFeatureClass fc)
{
    IFeature outFeat = null;
    IQueryFilter qf = new QueryFilterClass();
    qf.WhereClause = "LASTEDITOR = 'ESRI'";
    IFeatureCursor fCur = null;
    try
    {
        fCur = fc.Search(qf, false);
        outFeat = fCur.NextFeature();
    }
    catch (Exception ex)
    {
        var ex2 = new Exception("Exception when querying with " + qf.WhereClause, ex);
        throw ex2;
    }
    finally
    {
        if(fCur != null)
            Marshal.FinalReleaseComObject(fCur);
    }
    return outFeat;
}