2

I am using ArcMap 10.2.1 and ArcObjects 10.2.1, I just want to connect to a geodatabase and edit, add, delete a feature from a feature class using ArcObjects, I tried to connect to database and it works fine but I can't start editing and adding a feature to it.

Here is my code:

public void open_Workspace()
    {
    Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
    IWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IWorkspaceFactory2;
    // Build a connection string.
    String[] connectionProps = 
{
    "dbclient=SQLServer", "serverinstance=CRAZYVIRUS-HP", 
        "database=land_gis", "authentication_mode=OSA" 
};
    String connString = String.Join(";", connectionProps);

    IWorkspace workspace = workspaceFactory2.OpenFromString(connString, 0);
    ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
    ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("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;
    IWorkspaceEdit2 workspaceEdit2 = (IWorkspaceEdit2)workspaceEdit;
    IMultiuserWorkspaceEdit muWorkspaceEdit = (IMultiuserWorkspaceEdit)workspace;

    Make sure that non-versioned editing is supported. If not, throw an exception.
   if (!muWorkspaceEdit.SupportsMultiuserEditSessionMode
       (esriMultiuserEditSessionMode.esriMESMNonVersioned))
   {
       throw new ArgumentException(
           "The workspace does not support non-versioned editing.");
   }

   // Start a non-versioned edit session.
   muWorkspaceEdit.StartMultiuserEditing
   (esriMultiuserEditSessionMode.esriMESMNonVersioned);

    featureClass.CreateFeature();


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

}

When I run above code I got this error Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Geodatabase.IWorkspaceEdit2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{199A1F9D-435A-4118-9B3F-4E0B8F984AA0}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I tried to enable versioning or disable it but there is not any chance to make it work... Can you help me find the solution into this error? Is there any thing wrong with my connection?

  • I've no experience with SQLserver in this manner, however for PostgreSQL you use an IPropertySet to connect. Be sure that your IWorkspace supports IWorkspaceEdit, I have a suspicion that it may not, hence the error. I'm confused about the Activator.CreateInstance here, can you confirm that is correct... normally it would be IWorkspaceFactory workspaceFactory = new SqlWorkspaceFactory(); but like I said, no experience in this, it may not be a problem. – Michael Stimson Jul 13 '14 at 23:06
  • @MichaelMiles-Stimson You know..I am new with arcobject :( but I can connect to database and work with feature layer with this code..Can you halp me and share an example code that you use with postsql?Maybe I get an idea how to make this code works. thanks – Majid Jul 14 '14 at 5:38
  • I don't use SqlWorkspaceFactory I use SDEworkspaceFactory help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/… then IWorkspaceFactory.Open using an IPropertySet with the connection details. Not every workspace type will support all methods. I think that returned workspace does not support IWorkspaceEdit2, stick with IWorkspaceEdit. – Michael Stimson Jul 14 '14 at 5:41
  • I am sure this is just a snippet because your featureClass just appears from nowhere. CreateFeature() returns an IFeature which needs to have the Store() invoked before it actually appears in the database. This works but I recommend using IFeatureBuffer / IFeatureCursor / IFeatureClass.InsertCursor() and .CreateFeatureBuffer() - not much more effort and faster. – Michael Stimson Jul 14 '14 at 5:46
  • @MichaelMiles-Stimson Yes I am trieng to learn how edit feature and then use in in my code,So what key words do I search to find about this IFeatureBuffer and other interfeces,and also I tried to use SDEworkspaceFactory before but I faced lots of errors :(I asked a question about it here:gis.stackexchange.com/questions/104656/… – Majid Jul 14 '14 at 5:57
1

I think @MichaelMiles-Stimson is right and you should use the SdeWorkspaceFactory instead of SqlWorkspaceFactory and then solve the following errors you mentioned.

The examples are here and this way works in our project.

  • Thanks for your help..Can you give me a source to setup arcsde?I installed in on client side but I cant connect to it..Is is client side?or server side? – Majid Jul 14 '14 at 14:23
  • ArcSDE is part of ArcGis server enterprise and is very expensive. If it is an option then you must get the client libraries to suit your database from Esri support. The SqlWorkspaceFactory exists but might only support tables, therefore not the IWorkspaceEdit. I would do more research into it unless of course you already have SDE. – Michael Stimson Jul 14 '14 at 21:33
  • Yes, you definitely must have the server side to connect as to SDE. ArcSDE itself is also known as ArcGIS for Server Basic (Enterprise or Workgroup) and also is a part of greater products. You can contact your local ESRI distributor to get a test license or buy a Term license, it is cheaper. Another way to test is try to use ArcSDE Personal which comes with ArcGIS for Desktop Standard or Advanced (You need to have at least Standard license to edit SDE data). – gnevan Jul 15 '14 at 6:44
  • @gnevan thanks for your good information,I think I can not have SDE licence so I must find a way to connect to geoserver and edit featureclasses ..I can edit tabels using native sql server abilities but I ned editing features..The only way to do this is using SDE?I have ArcMap Advance licence so this is why I can connect to database using arcmap without any problem but can not connect using my arcEngine application:( – Majid Jul 15 '14 at 10:38
  • I don't understand what database or what features you want to edit if you don't have SDE? – gnevan Jul 15 '14 at 10:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.