AnsweredAssumed Answered

Programmatically  Adding layer to the Arcmap Using C#

Question asked by geonetadmin on Sep 26, 2010
Latest reply on Nov 28, 2012 by kjnether
Original User: Shrey

Hi All,


     After the user Login to the system I am Invoking the Arcmap by a function. While doing so I am also calling the function to Add the shapefile required in my application .For this I am using the function as mentioned below :



   public void StartArcMap()
        {
            if (m_pDoc == null)
            {
                // Cursor.Current = Cursors.WaitCursor;

                //Start arcmap
                CheckLicence();
                m_pDoc = new MxDocumentClass();

                //Get a reference to the application
                m_application = m_pDoc.Parent;



                m_appHWnd = m_application.hWnd;
                m_application.Caption = "Registeration System V 3.0";
                m_pMxdocument = m_application.Document as IMxDocument;
               
             
                //Show arcmap
                m_application.Visible = true;
               
                //Calling to Add The Shape File
                AddShapefile();

                //Hide All toolbars of Arcmap
              //  m_application.Document.CommandBars.HideAllToolbars();



             
                //Get the commandbars collection
                ICommandBars pCmdBars;
                pCmdBars = m_application.Document.CommandBars;
                //Create New ToolBar
                ICommandBar pNewBar;
                pNewBar = pCmdBars.Create("Adminstrator", ESRI.ArcGIS.SystemUI.esriCmdBarType.esriCmdBarTypeToolbar);

                UIDClass clsSearch = new UIDClass();
                clsSearch.Value = "Tools.clsSearch";
                int intclsSearch = pNewBar.Count;
                object objclsSearch = intclsSearch;
                pNewBar.Add(clsSearch,ref objclsSearch);  

 
                 
             



             
            }
        }//Start the Arcmap Application


  public void AddShapefile()
        {

         
            try
            {


                //IWorkspace pworkspace;
                IWorkspaceFactory pWorkspaceFactory;
                IFeatureWorkspace pFeatureWorkSpace;
                IFeatureClass pFeatureClass;
                IGeoFeatureLayer pGeoFeatureLayer;

                pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                //pFeatureWorkSpace = (IFeatureWorkspace)pWorkspaceFactory;
                pFeatureWorkSpace = pWorkspaceFactory.OpenFromFile(@"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\AirportsAndGolf", 0) as IFeatureWorkspace;

                pFeatureClass = pFeatureWorkSpace.OpenFeatureClass("STATE");
                pGeoFeatureLayer = new FeatureLayerClass();
                pGeoFeatureLayer.Name = pFeatureClass.AliasName;
                pGeoFeatureLayer.FeatureClass = pFeatureClass;

                m_pMxdocument.AddLayer((ILayer)pGeoFeatureLayer);

                // Ading the Road LAyer
                pFeatureWorkSpace = pWorkspaceFactory.OpenFromFile(@"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\AirportsAndGolf", 0) as IFeatureWorkspace;

                pFeatureClass = pFeatureWorkSpace.OpenFeatureClass("AIRPORT");
                pGeoFeatureLayer = new FeatureLayerClass();
                pGeoFeatureLayer.Name = pFeatureClass.AliasName;
                pGeoFeatureLayer.FeatureClass = pFeatureClass;

                m_pMxdocument.AddLayer((ILayer)pGeoFeatureLayer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(""+ex);

            }
 
        }//End OF Adding the Shapefile


But while doing so the I cannot open the shapefiles and system shows the following error (Attachment one) and if I try some other shape files it Arcmap will shut down and the Error 2 will be show (Attachment)


Please require your help desperately. Thank You

Outcomes