Select to view content in your preferred language

Strange cursor problem

853
2
01-21-2011 10:57 AM
BBulla
by
Regular Contributor
OK,

So I am using a cursor to read a field value from a selected item in a shapefile.  I need this value, so that I can update values in an external spreadsheet.

After I read the value, I need to update it with a new value, which I am using another cursor for.  For some reason, this isn't working.  I think it's because I'm using more than one curosr on the same shapefile, but I can't say for sure.

Here is my code when I am reading the shapefile:

if (pLayer.Name == GlobalVars.TabletHydrantLayer)
                {
                    featureSelection = (IFeatureSelection)pLayer;
                    selectionSet = (ISelectionSet2)featureSelection.SelectionSet;

                    if (selectionSet.Count == 1)
                    {
                        selectionSet.Search(null, false, out cursor);
                        featureCursor = (IFeatureCursor)cursor;
                        feature = featureCursor.NextFeature();

                        GlobalVars.SHPHydrantFacilityID = feature.get_Value(feature.Fields.FindField("FACILITYID")).ToString();      
                    }
                    else
                    {
                        MessageBox.Show("Please make sure one feature is selected from the Tablet Hydrant layer " + GlobalVars.TabletHydrantLayer);
                        return;
                    }
                }



And here is my code when I am trying to write to the same shapefile:

if (pLayer.Name == GlobalVars.TabletHydrantLayer)
                {
                    //Get the selected features of the current layer.
                    featureLayer = (IGeoFeatureLayer)pLayer;
                    featureSelection = (IFeatureSelection)featureLayer;
                    selectionSet = (ISelectionSet2)featureSelection.SelectionSet;

                    if (selectionSet.Count == 1)
                    {
                        //Create a feature cursor, so we can loop through all the selected features
                        selectionSet.Update(null, false, out cursor);
                        featureCursor2 = (IFeatureCursor)cursor;

                        //Start at the first selected feature.
                        feature = featureCursor2.NextFeature();
                        feature.set_Value(feature.Fields.FindField("FACILITYID"), "THIS IS A TEST");
                        featureCursor2.UpdateFeature(feature);
                    }
//Stop editing
            m_editor.StopOperation("GIF Angle Update");

            //Once all layers are processed, update the selected features on the map
            ISelectionEvents selEvents = (ISelectionEvents)map;
            selEvents.SelectionChanged();

            MessageBox.Show("DONE");


The weirdest thing of all, is that when this code runs the first time (on a click event of a command button), then it doesn't work, but if I click it again, then it does.

Any ideas as to what is going on??  If I don't even include the first bit of code where I read from the shapefile, it all works fine, but once I add it back, it doesn't work again.  No error messages, it just doesn't work.

I've been working on this for about 2 days and it's driving me insane!!!
0 Kudos
2 Replies
NeilClemmons
Honored Contributor
The first thing I see is that you are not releasing the cursor and feature references.  It is very important that you do so.  A typical code block for querying a feature class and processing the cursor would look something like this:

Dim featureCursor As IFeatureCursor = featureClass.Search(Nothing, False)
Dim feature As IFeature = featureCursor.NextFeature
Do While feature IsNot Nothing
  ' do something with the feature

  Marshal.FinalReleaseComObject(feature)
  feature = featureCursor.NextFeature
Loop
Marshal.FinalReleaseComObject(featureCursor)


Basically, you should be releasing the cursor and row references immediately after you are done with them.  This includes releasing Feature and Row objects within loops BEFORE setting them to the next object in the cursor.  Release the cursor references as soon as you're finished with the Feature/Row objects.
0 Kudos
BBulla
by
Regular Contributor
Neil,

As always, you're a genius!!  I tried setting it to Null, but that never worked.  I've never heard of Marshal.FInalReleaseComObject, but that seems to do the trick.  I'll have to use that more often!!

Thanks!
0 Kudos