What am I doing wrong on this insert cursor?

2669
8
Jump to solution
10-07-2013 08:26 AM
JohnStephens
Occasional Contributor
I have a QC process that runs through some data, flags various features, and then I want it to write them out to a new database with an exact copy of the feature class (I can't copy the feature class completely, because not all of the features are flagged).  My code for flagging the data works fine, but when I go to insert my created feature buffer I get a vague COM Exception. 

I've read that these errors usually occur with field/field value errors, but I've used a IFieldChecker, which reports no errors.  And I've gone through the created feature class fie and the values in the feature

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); }             }         }     } }


Also, I'm not sure if it is related, but when I am debugging the code the execute function to check the members of the insert cursor does not work.  I am assuming it's because the cursor has an exclusive lock on the data.
0 Kudos
1 Solution

Accepted Solutions
JohnStephens
Occasional Contributor
Thanks for the help guys.  After a lot of head banging on the keyboard, I finally found that it was an error with my threading.  I created the database I was writing to outside of the current thread and that was the issue. 

Anyway, I'll give you guys some points for giving me some suggestions.

View solution in original post

0 Kudos
8 Replies
DuncanHornby
MVP Notable Contributor
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:

  • 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?

Also can you confirm your queryfilter is actually selecting data?
0 Kudos
JohnStephens
Occasional Contributor
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:

  • 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?

Also can you confirm your queryfilter is actually selecting data?


Thanks for the reply.  The error message is really vague: Error: The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) Exception: System.Runtime.InteropServices.COMException. 

I was thinking the error might be with the newFC as well.  I created a copy of the source DB, deleted the features from it, and tried to write to that and I got the same error.  So, it has to be something going awry with the insert and/or values going into fields.  Though, like I said, I went through and checked every value going into the first feature buffer and they all match the field types.

I can confirm the queryfilter is returning data.  I took out a line of code that skips feature classes when the queryfilter returns nothing:

if (currFeat == null) continue; 
0 Kudos
DuncanHornby
MVP Notable Contributor
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?
0 Kudos
JohnStephens
Occasional Contributor
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?


Yeah, I don't know why it's talking about a server.  I'm doing nothing with ArcSever.  Everything is on my C drive right now and this is just a stand-alone desktop application.
0 Kudos
NeilClemmons
Regular Contributor III
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).
0 Kudos
JohnStephens
Occasional Contributor
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).


Thanks for responding, Neil.  I had added that var assignment just for debugging.  I forgot to take it out when I posted the code.

Anyway, I used ShapeCopy and added a check to see if any of the fields were not editable.  They were all editable and I still hit the same error.

I also tried opening an IWorkspaceEdit session and that did nothing.
0 Kudos
DuncanHornby
MVP Notable Contributor
OK well if this was me I would try to start to narrow down what is causing the problem? Try just writing the shape, if code runs then it suggests its to do with the transferring of the remaining attributes. Then try the reverse and if that is inconclusive try just writing blank rows?
0 Kudos
JohnStephens
Occasional Contributor
Thanks for the help guys.  After a lot of head banging on the keyboard, I finally found that it was an error with my threading.  I created the database I was writing to outside of the current thread and that was the issue. 

Anyway, I'll give you guys some points for giving me some suggestions.
0 Kudos