VB .Net : Layer Adds Ok But No Data .. then arcmap crashes

Discussion created by ansergis on Mar 29, 2012

I am trying to simply add a layer in programatically. The layer appears in ArcMap, but there is no data there in. If I try to look at layer properties, ArcMap crashes.

I've tried both shape and Access workspacefactory , and various shape files in various paths (no spaces etc..) but the same problem. Refreshing the data doesn't make any difference.

I can see from the featurelayer properties that the correct data has been found, with the correct number of fields. What am I doing wrong?



    Private Sub AddShapefile(ByRef m_Application)
        Dim mapDoc As IMapDocument = m_Application.Document
        ' Create a ShapefileWorkspaceFactory object and
        ' open a shapefile folder. The path works with a standard 9.3 installation.
        Dim AccworkspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactoryClass()
        Dim featureWorkspace As IFeatureWorkspace = TryCast(AccworkspaceFactory.OpenFromFile("C:\WindaWorkSpace\Data\WindaPGdb.mdb", 0), IFeatureWorkspace)

        ' Create a FeatureLayer and assign a shapefile to it.
        Dim featureLayer As IFeatureLayer = New FeatureLayerClass
        featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass("winda")
        Dim layer As ILayer = featureLayer
        layer.Name = featureLayer.FeatureClass.AliasName
        ' Add the layer to the focus map.
        Dim pMap As ESRI.ArcGIS.Carto.IMap = mapDoc.ActiveView.FocusMap
        Dim t As Integer
        t = 1
    End Sub

EDIT : Once again Neil Clemmens saves the day.. When automating from an external application you need to use the IObjectsFactory :

Given that most people must now be using VS 2010 or similar to develop, this should be written in big red letters the first page of the esri user help pages!