Add-In to change MXD Data Source Only Works if User Saves & Exits MXD After Execution

2216
6
Jump to solution
03-26-2014 08:47 AM
E__C_Roman
New Contributor II
Hello,

I have created an ArcMap 10.1 Add-In in C# which allows a user to select a data source from a combo box and then modifies all layers to this new source.  After the add-in completes execution, viewing the layer Source Properties indicates the Instance value did not change.  However, if the MXD is closed and the user answers Yes to the "Save changes ...mxd" prompt seen in the AcrMap window, when the MXD is re-opened, the new data source is seen.

How can the change be seen on the layers without having to close and then re-open the MXD?  I have tried various refresh options (i.e. ContentsView, ActiveView) but they didn't work.

Thank you in advance.
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
How are you changing the data source?  Since you're updating the data connection while the document is open, I would do this by calling IDataLayer2.Disconnect first.  Then call IDataLayer2.Connect to restore the connection.  You might also try calling IApplication.SaveDocument after updating the data source.

View solution in original post

0 Kudos
6 Replies
NeilClemmons
Regular Contributor III
Have you called IMxDocument.UpdateContents?
0 Kudos
E__C_Roman
New Contributor II
Yes, this has been done.
0 Kudos
NeilClemmons
Regular Contributor III
How are you changing the data source?  Since you're updating the data connection while the document is open, I would do this by calling IDataLayer2.Disconnect first.  Then call IDataLayer2.Connect to restore the connection.  You might also try calling IApplication.SaveDocument after updating the data source.
0 Kudos
E__C_Roman
New Contributor II
I change each workspace name's connection properties to the new property set:

            dataLayer = (IDataLayer)featureLayer;
            datasetName = (IDatasetName)dataLayer.DataSourceName;
            workspaceName = (IWorkspaceName)datasetName.WorkspaceName;
            workspaceName.ConnectionProperties = newMXDPropertySet;

I am not clear on how to get the Application or DataLayer from the MXDocument information I have; can you please include the code to do this?

Thank you.
0 Kudos
MichaelVolz
Esteemed Contributor
I have the same problem with an application I created, so I am also interested in a solution to this problem.
0 Kudos
E__C_Roman
New Contributor II
Thank you!!  Disconnecting and then connecting worked:
         
            IDataLayer dataLayer;
            IDataLayer2 dataLayer2;
            IDatasetName datasetName;
            IWorkspaceName workspaceName;

            dataLayer = (IDataLayer)featureLayer;
            dataLayer2 = (IDataLayer2)featureLayer;
            dataLayer2.Disconnect();     <-------------------------------------**
            datasetName = (IDatasetName)dataLayer.DataSourceName;
            workspaceName = (IWorkspaceName)datasetName.WorkspaceName;
            workspaceName.ConnectionProperties = newMXDPropertySet;

            dataLayer2.Connect(null);     <------------------------------------- **
0 Kudos