salvador37

IFeatureLayer.FeatureClass returning null

Discussion created by salvador37 on Aug 18, 2011
Latest reply on Aug 25, 2011 by agray1
Hi everyone

This might be a simple doubt. When opening a single .mxd, I can read correctly some properties of the layer, like Name and DataSourceType. When I try to access the FeatureClass, it always returns null. Checking the source inside ArcMap, the FeatureClass is there just as I want. So, why can't I read it programatically?

Here's the relevant part of the code:

namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
    private IMapDocument pMapDocument = null;
    private IMap pMap = null;
    private IEnumLayer pEnumLayer = null;
    private IFeatureLayer pFeatureLayer = null;
    private IDataLayer2 pDataLayer = null;
    private IDatasetName pDatasetName = null;
    private IWorkspace pWorkspace = null;
    private IWorkspaceName pWorkspaceName = null;
    private IUID pUID = null;
    private string p = "password";

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
      Application.DoEvents();
      Start("", true);
    }

    private void Start(string sPath, bool inicializando)
    {
      bool saveInsideLayer = true;
      int i;
      string mxd;

      IPropertySet pConnectionProperties = new PropertySetClass();

      if (sPath == "")
      {
        saveInsideLayer = false;
        sPath = @"C:\layer.mxd";
      }

      try
      {
        pMapDocument = new MapDocumentClass();
        if (pMapDocument.get_IsPresent(sPath))
        {
          pMapDocument.Open(sPath, "");
        }
        
        for (i = 0; i < pMapDocument.MapCount; i++)
        {
          pMap = pMapDocument.get_Map(i);

          pUID = new UIDClass();
          pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; 
          pEnumLayer = pMap.get_Layers((UID)pUID, true);

          pEnumLayer.Reset();
          pFeatureLayer = (IFeatureLayer)pEnumLayer.Next();

          while (pFeatureLayer != null)
          {
            if (pFeatureLayer.DataSourceType.Contains("SDE"))
            {
              pDataLayer = (IDataLayer2)pFeatureLayer;
              if (pDataLayer.DataSourceName is IDatasetName)
              {
                pDatasetName = (IDatasetName)pDataLayer.DataSourceName;
                pWorkspaceName = pDatasetName.WorkspaceName;
                if (pFeatureLayer.FeatureClass != null) // why never true?
                {
                  pConnectionProperties = pFeatureLayer.FeatureClass.ExtensionProperties; 
                }

                pWorkspaceName.ConnectionProperties = pConnectionProperties;
              }
            }
            pFeatureLayer = (IFeatureLayer)pEnumLayer.Next();
          }

          txtServidor.Text = pConnectionProperties.GetProperty("SERVER").ToString();
          txtPorta.Text = pConnectionProperties.GetProperty("INSTANCE").ToString();
          txtDatabase.Text = pConnectionProperties.GetProperty("DATABASE").ToString();
          txtUsuario.Text = pConnectionProperties.GetProperty("USERNAME").ToString();
          txtSenha.Text = pConnectionProperties.GetProperty("PASSWORD").ToString();

          if (saveInsideLayer)
          {
            pMapDocument.ReplaceContents((IMxdContents)pMap);
            pMapDocument.Save();
            pMapDocument.Close();
          }
        }
      }
      catch (Exception ex)
      {
        throw new Exception("Unable to run the code", ex);
      }
    }

  }
}


I'm working in Visual Studio 2005, and using an ArcInfo license for 9.3 version.

Thanks for the attention

Rodrigo Salvador

Outcomes