using (ComReleaser comReleaser = new ComReleaser()) { IFeatureCursor featuresToCopy = fc.Search(qf, true); comReleaser.ManageLifetime(featuresToCopy); IFeature currFeat = featuresToCopy.NextFeature(); IFeatureCursor insertCursor = newFC.Insert(true); comReleaser.ManageLifetime(insertCursor); while (currFeat != null) { IFeatureBuffer featBuff = newFC.CreateFeatureBuffer(); // Add the original feature's geometry to the feature buffer. featBuff.Shape = currFeat.Shape; // Add all the original feature's fields to the feature buffer AddFieldsToFeatureBuffer(ref featBuff, currFeat); try { insertCursor.InsertFeature(featBuff); } catch { throw; } currFeat = featuresToCopy.NextFeature(); } } //This code was taken right from here private void AddFieldsToFeatureBuffer(ref IFeatureBuffer featBuffer, IFeature feature) { // Copy the attributes of the orig feature the new feature IRowBuffer rowBuffer = (IRowBuffer)featBuffer; IFields fieldsNew = rowBuffer.Fields; IFields fields = feature.Fields; for (int i = 0; i <= fields.FieldCount - 1; i++) { IField field = fields.get_Field(i); if ((field.Type != esriFieldType.esriFieldTypeGeometry) && (field.Type != esriFieldType.esriFieldTypeOID)) { int intFieldIndex = fieldsNew.FindField(field.Name); if (intFieldIndex != -1) { try { var value = feature.get_Value(i); featBuffer.set_Value(intFieldIndex, value); } catch (Exception ex) { WriteError(ex); } } } } }
Solved! Go to Solution.
Without seeing the actual error message and as the code looks OK to me I'm guessing the problem is to do with your newFC.
Are you trying to:Also can you confirm your queryfilter is actually selecting data?
- Insert incorrect geometries (e.g. storing a polyline in a point FeatureClass)?
- Does the source featureclass have a different spatial reference?
- Does the source featureclass support MZ values and your newFC does not?
if (currFeat == null) continue;
The error hints at a server, are you storing data in ArcServer? If so it might be some weird permissions problem, if it is then I have to defer to someone else with more knowledge of ArcServer (as I have not used it). So that in mind can you try putting your newFc in a file geodatabase and try writing to that?
3 things I would try first: use ShapeCopy instead of Shape to set the geometry, add a check to the field to make sure it's editable before trying to copy the field value and combine the field value assignment to not use a variable (feature.set_Value = feature.get_value).