1

I have created an IFeatureCursor using IFeatureClass.Search(). The features in this exist and are not null. However, when I pass that same cursor into another method there are suddenly no features. Why is this happening?

public static void ConvertCursor(string workSpace, string featureClass, System.Data.DataTable unmatchedPointsDataTable)
    {
        IWorkspaceFactory workspaceFactory = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")) as IWorkspaceFactory;
        IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(workSpace, 0) as IFeatureWorkspace;
        IFeatureClass unMatchedFeatureClass = featureWorkspace.OpenFeatureClass(featureClass);
        using (ComReleaser comReleaser = new ComReleaser())
        {
            IQueryFilter matchedQueryFilter = new QueryFilterClass();
            IFeatureCursor matchedSearchCursor = unMatchedFeatureClass.Search(matchedQueryFilter, false);

            CreateFeatureClassFromCursor(workSpace, featureClass, matchedSearchCursor, unMatchedFeatureClass.Fields);

            matchedSearchCursor.Flush();
        }


    }

    public static void CreateFeatureClassFromCursor(string workSpace, string featureClass, IFeatureCursor cursor, IFields fieldCollection)
    {
        try
        {
            IWorkspaceFactory workspaceFactory = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")) as IWorkspaceFactory;
            IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(workSpace, 0) as IFeatureWorkspace;
            IFeatureWorkspaceManage featureWorkspaceManage = featureWorkspace as IFeatureWorkspaceManage;

            IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
            IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;

            // Use IFieldChecker to create a validated fields collection.
            IFieldChecker fieldChecker = new FieldCheckerClass();
            IEnumFieldError enumFieldError = null;
            IFields validatedFields = null;
            fieldChecker.ValidateWorkspace = featureWorkspace as IWorkspace;
            fieldChecker.Validate(fieldCollection, out enumFieldError, out
              validatedFields);

            try
            {
                if (featureWorkspace.OpenFeatureClass(featureClass).HasOID)
                {
                    IWorkspaceName workspaceName = new WorkspaceNameClass();
                    workspaceName.PathName = workSpace;
                    workspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory";
                    IFeatureClassName featureClassName = new FeatureClassNameClass();
                    IDatasetName datasetName = (IDatasetName)featureClassName;
                    datasetName.Name = featureClass;
                    datasetName.WorkspaceName = workspaceName;
                    featureWorkspaceManage.DeleteByName(datasetName);
                }
                if (featureWorkspace.OpenFeatureClass(featureClass).HasOID)
                {
                    MessageBox.Show("FeatureClass " + featureClass + " already exists and could not be deleted. Please close all programs using this FeatureClass and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch
            {

            }

            IFeatureClass newFeatureClass = featureWorkspace.CreateFeatureClass
              (featureClass, validatedFields, ocDesc.InstanceCLSID,
              ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, "shape",
              "");

            using (ComReleaser comReleaser = new ComReleaser())
            {
                // Create a feature buffer.
                IFeatureBuffer featureBuffer = newFeatureClass.CreateFeatureBuffer();
                comReleaser.ManageLifetime(featureBuffer);

                // Create an insert cursor.
                IFeatureCursor insertCursor = newFeatureClass.Insert(true);
                comReleaser.ManageLifetime(insertCursor);

                IFeature feature = cursor.NextFeature();
                int intFeatureCount = 0;
                while (feature != null)///////////THIS IS NULL!!!!!WHY?
                {
                    // Add the original feature's geometry to the feature buffer.
                    featureBuffer.Shape = feature.Shape;

                    // Add all the original feature's fields to the feature buffer.
                    AddFields(featureBuffer, feature);

                    // Insert the feature into the cursor.
                    insertCursor.InsertFeature(featureBuffer);

                    // Flush the feature cursor every 100 features.
                    // This is safer because you can write code to handle a flush error
                    // If you don't flush the feature cursor it will automatically flush but
                    // after all of your code executes at which time you have no control.
                    if (++intFeatureCount == 100)
                    {
                        insertCursor.Flush();
                        intFeatureCount = 0;
                    }

                    // Get Next Feature
                    feature = cursor.NextFeature();
                }

                // Flush the cursor one last time
                insertCursor.Flush();
            }
        }
        catch
        {
            throw;
        }
    }
  • Have you checked if it is null before you pass it to the other method? You haven't defined your query, i.e. matchedQueryFilter.WhereClause = ... – Barbarossa Mar 16 '17 at 20:23
  • @Barbarossa I have checked and it is not null before passing it into the other method. I left the query undefined because I want to return everything. – Programmer Mar 16 '17 at 20:27
  • Have you tried cursor.Reset() prior to looping? – Barbarossa Mar 16 '17 at 20:32
  • 1
    Very likely, as the cursor is only referencing the FC. – Barbarossa Mar 16 '17 at 21:19
  • 5
    Yes, the cursor is only a reference to the features in the feature class.. if you delete them they're gone, if you delete the feature class then the cursor points to nothing. If you need to replace the feature class then write the features to a random non-existent name in the same workspace and when done delete the feature class and rename resources.arcgis.com/en/help/arcobjects-net/componenthelp/… the new one... note that rename is not supported by some workspaces. Be sure to check IDataset.CanRename first, if you can't rename then delete and copy. – Michael Stimson Mar 16 '17 at 21:39

Your Answer

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

Browse other questions tagged or ask your own question.