Layer properties changed, but not opening in ArcMap

1368
1
08-25-2011 05:52 AM
RodrigoSalvador
New Contributor
Hi everyone

I reached my goal in changing layer properties, it's greatly working. My code opens a .mxd, look for layer properties, load all of them for each layer, change the convenient ones and save these changes.

Here's the trouble: Before running the code, the layers are correctly loaded when opening ArcMap, and the DataSource contains all connection properties and spatial references. After it, the red exclamation appears. Taking a look at the DataSource, the connection properties are there (with some differences, like the password field, which was hidden before and now is shown), but the spatial references are gone.

So, my questions are: Am I loading it wrong? Is a DataSource connecting nedded in the code? What's the reason to that?

PS: I tried to use IDataLayer2.Connect() and Disconnect() methods, but they're giving me COM exceptions.

        this.pMapDocument = new MapDocumentClass();
        if (!this.pMapDocument.get_IsPresent(sPath))
        {
          MessageBox.Show("The file " + sPath + " doesn't exists");
          return;
        }

        this.pMapDocument.Open(sPath, "");

        for (i = 0; i < this.pMapDocument.MapCount; i++)
        {
          this.pMap = new MapClass();
          this.pMap = this.pMapDocument.get_Map(i);
          this.pMapDocument.ActiveView.Activate(i);
          Application.DoEvents();

          this.pUID = new UIDClass();
          this.pUID.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
          this.pEnumLayer = this.pMap.get_Layers((UID)pUID, true);

          this.pEnumLayer.Reset();
          this.pFeatureLayer = (IFeatureLayer)this.pEnumLayer.Next();
          
          while (this.pFeatureLayer != null)
          {
            string dst = this.pFeatureLayer.DataSourceType;
            if (dst.Contains("SDE"))
            {
              this.pDataLayer = (IDataLayer2)this.pFeatureLayer;

              this.pDatasetName = (IDatasetName)this.pDataLayer.DataSourceName;
              this.pWorkspaceName = this.pDatasetName.WorkspaceName;
              this.pConnectionProperties = this.pWorkspaceName.ConnectionProperties;

              if (!starting)
              {
                Application.DoEvents();
                
                this.pConnectionProperties.SetProperty("SERVER", this.txtServidor.Text);
                this.pConnectionProperties.SetProperty("INSTANCE", this.txtPorta.Text);
                this.pConnectionProperties.SetProperty("DATABASE", this.txtDatabase.Text);
                this.pConnectionProperties.SetProperty("USER", this.txtUsuario.Text);
                this.pConnectionProperties.SetProperty("PASSWORD", this.txtSenha.Text);

                this.pWorkspaceName = new WorkspaceNameClass();
                this.pWorkspaceName.ConnectionProperties = this.pConnectionProperties;
                this.pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.SdeWorkspaceFactory";

                this.pNome = (IName)this.pWorkspaceName;
                this.pNome.NameString = "SDE Connection";

                this.pDatasetName = (IDatasetName)this.pDataLayer.DataSourceName;
                this.pDatasetName.WorkspaceName = this.pWorkspaceName;
              }
              else
              {
                Application.DoEvents();

                this.txtServidor.Text = this.pConnectionProperties.GetProperty("SERVER").ToString();
                this.txtPorta.Text = this.pConnectionProperties.GetProperty("INSTANCE").ToString();
                this.txtDatabase.Text = this.pConnectionProperties.GetProperty("DATABASE").ToString();
                this.txtUsuario.Text = this.pConnectionProperties.GetProperty("USER").ToString();
              }
              this.pFeatureLayer = (IFeatureLayer)this.pEnumLayer.Next();
            }
          }
        }
        if (!starting)
        {
          this.pMapDocument.ReplaceContents((IMxdContents)this.pMap);
          this.pMapDocument.Save(true, false);
        }
        this.pMapDocument.Close();
      } 


Thanks for your attention

Rodrigo Salvador
0 Kudos
1 Reply
RodrigoSalvador
New Contributor
Hi again

Please, ignore this thread. The problem was internal (I was reading the wrong password). It's working fine. Sorry about that.

Rodrigo Salvador
0 Kudos