AnsweredAssumed Answered

Insert Feature duplicated and ...

Question asked by shaningesri on May 30, 2012
Latest reply on Jun 2, 2012 by shaningesri
In the main class, after I click a point on the map, the OnMouseDown(int Button, int Shift, int X, int Y) event is fired, and a empty record is created.  Meanwhile, the associated form pops out.  After I enter the values (e.g. street ID, street name)  into the form, I click the OK button to insert the record.  The component used is revised from the source code at http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeoDatabase/IFeatureClass_Insert.htm.

        private void btnOK_Click(object sender, EventArgs e)    {
            FClassInsert(pFL.FeatureClass, "STREET_ID", intStreetID,
                "ADDRESS_NU", Convert.ToInt32(strHNo), "STREET_NM", strName);
            this.Close();   
        }

        public void FClassInsert(IFeatureClass featureClass,
            string sField1, int intValue1, string sField2, int intValue2, string sField3, string sValue)  {
            //get the Workspace from the IDataset interface on the feature class
            IDataset dataset = (IDataset)featureClass;
            IWorkspace workspace = dataset.Workspace;

            //Cast for an IWorkspaceEdit
            IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;

            //Start an edit session and operation
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();

            //Create the Feature Buffer
            IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
            //Create insert Feature Cursor using buffering = true.
            IFeatureCursor featureCursor = featureClass.Insert(true);

            object featureOID;

            //With a feature buffer you have the ability to set the attribute for a specific field to be
            //the same for all features added to the buffer.
            featureBuffer.set_Value(featureBuffer.Fields.FindField(sField1), intValue1);
            featureBuffer.set_Value(featureBuffer.Fields.FindField(sField2), intValue2);
            featureBuffer.set_Value(featureBuffer.Fields.FindField(sField3), sValue);

            //Here you can set the featurebuffers's shape by setting the featureBuffer.Shape
            //to a geomerty that matched the featureclasses.

            //Insert the feature into the feature cursor
            featureOID = featureCursor.InsertFeature(featureBuffer);

            //Calling flush allows you to handle any errors at a known time rather then on the cursor destruction.
            featureCursor.Flush();

            //Stop editing
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);

            //Release the Cursor
            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
        }

The output results are below:
FID     Shape    STREET_ID   ADDRESS_NU     STREET_NM
0       Point      9997113               0           
1       Point      9997113          3455            LITTLE GRAVE CREEK DR

It means 2 records are created.  The first one is created after clicking a point (MouseDown event), anoter one is created after I stop the editing.  If you view this thread, can you find what's wrong in the code?  Thanks.

Outcomes