2

I have a problem with editing feature class using ArcObjects. Indeed I am working on it for a while and can't find any solution into doing it... I asked a question before about how to connect to database and I thought the problem is with it but I find another problem... Here is my code:

// getLayerFromGeoDatabase();
// Create the workspace factory.
        Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
        IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
        IWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IWorkspaceFactory2;
        // Build a connection string.
        String[] connectionProps = 
{
    "dbclient=SQLServer", "serverinstance=CRAZYVIRUS-HP", 
        "database=test", "authentication_mode=OSA" 
};
        String connString = String.Join(";", connectionProps);
        IPropertySet propertySet = new PropertySetClass();
        propertySet.SetProperty("SERVER", "CRAZYVIRUS-HP");
        //propertySet.SetProperty("INSTANCE", "CRAZYVIRUS-HP");
        propertySet.SetProperty("DATABASE", "land_gis");
        propertySet.SetProperty("authentication_mode", "OSA");
        //propertySet.SetProperty("PASSWORD", "123456");
        //propertySet.SetProperty("VERSION", "sde.DEFAULT");

        //IWorkspace workspace = workspaceFactory2.OpenFromString(propertySet, 0);
        IWorkspace workspace = workspaceFactory2.Open(propertySet, 0);
        ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
        ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("test.DBO.test");

        ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();
        featureLayer.FeatureClass = featureClass;
        featureLayer.Name = featureClass.AliasName;
        featureLayer.Visible = true;

        IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;

        // Start an edit session. An undo/redo stack isn't necessary in this case.
        workspaceEdit.StartEditing(false);

        // Start an edit operation.
        workspaceEdit.StartEditOperation();

        // Create a row. The row's attribute values should be set here and if
        // a feature is being created, the shape should be set as well.
        IFeature feature = featureClass.CreateFeature();
        feature.Store();

        // Save the edit operation. To cancel an edit operation, the AbortEditOperation
        // method can be used.
        workspaceEdit.StopEditOperation();

        // Stop the edit session. The saveEdits parameter indicates the edit session
        // will be committed.
        workspaceEdit.StopEditing(true);

I can connect to database but can't add features in it. I got a error in this line:

IFeature feature = featureClass.CreateFeature();

it says

enter code hereUnderlying DBMS error[[Microsoft][SQL Server Native Client 11.0][SQL Server]Violation of PRIMARY KEY constraint 'R7_pk'. Cannot insert duplicate key in object 'dbo.TEST'. The duplicate key value is (0).]

I can connect to database using ArcCatalog and also I have enabled versioning for featureclass in ArcCatalog... I am really confused with this problem... I have already used Create Enterprise Geodatabase for this database and also my feature class is empty. Sometimes I got this error on the same line.

I think there is something wrong with code... Some how this code creates a new feature but is says this new one is duplicated:(

Update

I managed to make this code work:

Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
        IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
        IWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IWorkspaceFactory2;

        IPropertySet propertySet = new PropertySetClass();
        propertySet.SetProperty("dbclient", "SQLServer");
        propertySet.SetProperty("DATABASE", "land_gis");
        propertySet.SetProperty("authentication_mode", "OSA");

        IWorkspace workspace = workspaceFactory2.Open(propertySet, 0);
        ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
        ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("test.DBO.test");

        ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();
        featureLayer.FeatureClass = featureClass;
        featureLayer.Name = featureClass.AliasName;
        featureLayer.Visible = true;

        IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
        workspaceEdit.StartEditing(false);
        workspaceEdit.StartEditOperation();

        IFeature feature = featureClass.CreateFeature();
        feature.Shape = polygon;

        ISubtypes subtypes = (ISubtypes)featureClass;
        IRowSubtypes rowSubtypes = (IRowSubtypes)feature;
        rowSubtypes.InitDefaultValues();

        int EnergyFieldIndex = featureClass.FindField("Energy");
        feature.set_Value(EnergyFieldIndex, property_info[1]);

        int corp_typeFieldIndex = featureClass.FindField("corp_type");
        feature.set_Value(corp_typeFieldIndex, property_info[2]);

        int PoolFieldIndex = featureClass.FindField("Pool");
        feature.set_Value(PoolFieldIndex, property_info[3]);

        int irrigationFieldIndex = featureClass.FindField("irrigation");
        feature.set_Value(irrigationFieldIndex, property_info[4]);

        int varietyFieldIndex = featureClass.FindField("variety");
        feature.set_Value(varietyFieldIndex, property_info[5]);

        int treesFieldIndex = featureClass.FindField("trees");
        feature.set_Value(treesFieldIndex, Convert.ToInt16(property_info[6]));

        int utilizatFieldIndex = featureClass.FindField("utilizat");
        feature.set_Value(utilizatFieldIndex, property_info[7]);

        int addressFieldIndex = featureClass.FindField("address");
        feature.set_Value(addressFieldIndex, property_info[9]);

        int water_hourFieldIndex = featureClass.FindField("water_hour");
        feature.set_Value(water_hourFieldIndex, Convert.ToDecimal(property_info[10]));

        int owner_IDFieldIndex = featureClass.FindField("owner_ID");
        feature.set_Value(owner_IDFieldIndex, Convert.ToInt16(property_info[11]));

        int w_sourceFieldIndex = featureClass.FindField("w_source");
        feature.set_Value(w_sourceFieldIndex, property_info[12]);

        int w_incheFieldIndex = featureClass.FindField("w_inche");
        feature.set_Value(w_incheFieldIndex, Convert.ToDecimal(property_info[13]));

        feature.Store();
        workspaceEdit.StopEditOperation();
        workspaceEdit.StopEditing(true);

I set feature.Shape = polygon; using this piece of code:

ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0);
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(string_ShapefileName);
Polygon p2 = featureClass.GetFeature(0).Shape as Polygon;
SelectedFeature = p2 as IPolygon;

I get an error that says Arithmetic overflow error converting expression to numeric

I think the problem is with one of myfields... How can I find which one makes this problem... I know that this error says that a field in the database is attempted to insert or update larger data of the same type or other data type but can't manage to find which one it is.

closed as off-topic by Andre Silva, whyzar, Vince, Jochen Schwarze, LaughU Feb 14 at 7:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – Andre Silva, whyzar, Vince, Jochen Schwarze, LaughU
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Your data, test, it exists doesn't it? This sounds like more of a database problem than an object problem. Note that SDE does not allow storing a feature with an empty geometry so creating a feature and storing it without setting the geometry is going to cause problems. – Michael Stimson Jul 22 '14 at 21:47
  • Dear @MichaelMiles-Stimson thanks for your answer..I solved my previous problem.But now I faced another one..Can you please take a look at my update in question?Thanks a lot – Majid Jul 23 '14 at 8:42
  • On what line does it give the error? Please don't use .Shape to read the geometry, always use .ShapeCopy, the use of .Shape may have unintended side effects. I am thinking that your Polygon should be IPolygon so Polygon p2 = featureClass.GetFeature(0).Shape as IPolygon; normally though it's IGeometry that is stored in the shape. – Michael Stimson Jul 23 '14 at 21:10
  • Dear @MichaelMiles-Stimson thanks for your great help..I found that the problem is in Area field..I did not get you about dont using .shape..you mean ' feature.ShapeCopy=polygon;'I am not sure that this works.? – Majid Jul 25 '14 at 17:55
  • No Majid, get the geometry from ShapeCopy, put the geometry into Shape: IPolygon pPol = (IPolygon)feature.ShapeCopy; to get feature.Shape = (IGeometry)pPol; to put. – Michael Stimson Jul 27 '14 at 21:57