<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Layers from SDE Database are shown as invalid in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid/m-p/1030290#M20251</link>
    <description>&lt;P&gt;Here is the scenario. I open an MXD using IMapDocument.Open. This MXD contains layers that read from our 10.2.1 Enterprise Geodatabase. I do some operation and then close the MapDocument using IMapDocument.Close(). I open another MXD that has layers from the same 10.2.1 Enterprise Geodatabase immediately thereafter and do a similar process. This time the process fails because it can't get the FeatureClass from the FeatureLayer.&amp;nbsp; I have created a small console app to demonstrate the problem. I am currently using ArcObjects 10.6.1. If you put a breakpoint at the line&amp;nbsp; mapDocument2.Close() and check the value of FeatureClass on FL2 it returns as null.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What have I missed here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using ESRI.ArcGIS.esriSystem;
using System;
using ESRI.ArcGIS.Carto;
using System.Runtime.InteropServices;

namespace TestApp
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new TestApp.LicenseInitializer();
        [System.Runtime.InteropServices.DllImport("user32")]
        public static extern int GetDesktopWindow();

        [STAThread()]
        static void Main(string[] args)
        {

            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced },
            new esriLicenseExtensionCode[] { });
            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()

            IMapDocument mapDocument = new MapDocumentClass();
            mapDocument.Open(@"\\pc.lojic.local\files\ArcGIS\10.6.1\Desktop\Development\Msd\Atlases\templates\forcemaincover.mxd");
            IActiveView pAV = (IActiveView)mapDocument.PageLayout;
            pAV.Activate(GetDesktopWindow());
            IFeatureLayer pFL = FindLayer(mapDocument.ActiveView.FocusMap, "ForceMains");
            Console.WriteLine(pFL.Name);

            pFL = null;
            pAV.Deactivate();
            pAV = null;
            mapDocument.Close();
            mapDocument = null;

            IMapDocument mapDocument2 = new MapDocumentClass();
            mapDocument2.Open(@"\\pc.lojic.local\files\ArcGIS\10.6.1\Desktop\Development\Msd\Atlases\templates\forcemains.mxd");
            IActiveView pAV2 = (IActiveView)mapDocument2.PageLayout;
            pAV2.Activate(GetDesktopWindow());
            IFeatureLayer pFL2 = FindLayer(mapDocument2.ActiveView.FocusMap, "ForceMains");
            mapDocument2.Close();
          
            m_AOLicenseInitializer.ShutdownApplication();
        }

       
        public static IFeatureLayer FindLayer(IMap pmap, string aLayerName)
        { 
            IEnumLayer pEnumLayer = pmap.Layers;
            ILayer pLayer;
            while ((pLayer = pEnumLayer.Next()) != null)
            {
                Console.WriteLine(pLayer.Name + " " + pLayer.Valid.ToString());
                if (pLayer.Name == aLayerName)
                    return (IFeatureLayer)pLayer;
            }
            return null;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 24 Feb 2021 23:47:53 GMT</pubDate>
    <dc:creator>ScottDickison1</dc:creator>
    <dc:date>2021-02-24T23:47:53Z</dc:date>
    <item>
      <title>Layers from SDE Database are shown as invalid</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid/m-p/1030290#M20251</link>
      <description>&lt;P&gt;Here is the scenario. I open an MXD using IMapDocument.Open. This MXD contains layers that read from our 10.2.1 Enterprise Geodatabase. I do some operation and then close the MapDocument using IMapDocument.Close(). I open another MXD that has layers from the same 10.2.1 Enterprise Geodatabase immediately thereafter and do a similar process. This time the process fails because it can't get the FeatureClass from the FeatureLayer.&amp;nbsp; I have created a small console app to demonstrate the problem. I am currently using ArcObjects 10.6.1. If you put a breakpoint at the line&amp;nbsp; mapDocument2.Close() and check the value of FeatureClass on FL2 it returns as null.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What have I missed here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using ESRI.ArcGIS.esriSystem;
using System;
using ESRI.ArcGIS.Carto;
using System.Runtime.InteropServices;

namespace TestApp
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new TestApp.LicenseInitializer();
        [System.Runtime.InteropServices.DllImport("user32")]
        public static extern int GetDesktopWindow();

        [STAThread()]
        static void Main(string[] args)
        {

            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced },
            new esriLicenseExtensionCode[] { });
            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()

            IMapDocument mapDocument = new MapDocumentClass();
            mapDocument.Open(@"\\pc.lojic.local\files\ArcGIS\10.6.1\Desktop\Development\Msd\Atlases\templates\forcemaincover.mxd");
            IActiveView pAV = (IActiveView)mapDocument.PageLayout;
            pAV.Activate(GetDesktopWindow());
            IFeatureLayer pFL = FindLayer(mapDocument.ActiveView.FocusMap, "ForceMains");
            Console.WriteLine(pFL.Name);

            pFL = null;
            pAV.Deactivate();
            pAV = null;
            mapDocument.Close();
            mapDocument = null;

            IMapDocument mapDocument2 = new MapDocumentClass();
            mapDocument2.Open(@"\\pc.lojic.local\files\ArcGIS\10.6.1\Desktop\Development\Msd\Atlases\templates\forcemains.mxd");
            IActiveView pAV2 = (IActiveView)mapDocument2.PageLayout;
            pAV2.Activate(GetDesktopWindow());
            IFeatureLayer pFL2 = FindLayer(mapDocument2.ActiveView.FocusMap, "ForceMains");
            mapDocument2.Close();
          
            m_AOLicenseInitializer.ShutdownApplication();
        }

       
        public static IFeatureLayer FindLayer(IMap pmap, string aLayerName)
        { 
            IEnumLayer pEnumLayer = pmap.Layers;
            ILayer pLayer;
            while ((pLayer = pEnumLayer.Next()) != null)
            {
                Console.WriteLine(pLayer.Name + " " + pLayer.Valid.ToString());
                if (pLayer.Name == aLayerName)
                    return (IFeatureLayer)pLayer;
            }
            return null;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 23:47:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid/m-p/1030290#M20251</guid>
      <dc:creator>ScottDickison1</dc:creator>
      <dc:date>2021-02-24T23:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Layers from SDE Database are shown as invalid</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid/m-p/1030324#M20252</link>
      <description>&lt;P&gt;Did you try running ReleaseCOMObject on all the non-null variables before calling ShutdownApplication?&lt;/P&gt;&lt;P&gt;Or something like this:&lt;/P&gt;&lt;P&gt;&lt;A href="https://stackoverflow.com/a/38170605/125400" target="_blank"&gt;https://stackoverflow.com/a/38170605/125400&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 04:39:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid/m-p/1030324#M20252</guid>
      <dc:creator>KirkKuykendall1</dc:creator>
      <dc:date>2021-02-25T04:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: Layers from SDE Database are shown as invalid</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid/m-p/1030459#M20253</link>
      <description>&lt;P&gt;I now have it working. Thank you for pointing me in the right direction!&lt;/P&gt;&lt;P&gt;I had tried using Marshal.ReleaseCOMObject to no avail before. The Stack Overflow post you referenced mentioned explicitly calling Garbage Collection and then waiting for it to complete. Specifically these two lines:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;GC.Collect();
GC.WaitForPendingFinalizers();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I placed those between the mapDocument.Close() and the mapDocument.Open() and it worked - at least in my test app. I'm gong to swtich back over to the project i"m working and and see if it works there.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 14:53:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid/m-p/1030459#M20253</guid>
      <dc:creator>ScottDickison1</dc:creator>
      <dc:date>2021-02-25T14:53:17Z</dc:date>
    </item>
  </channel>
</rss>

