Connects the Layer to an underlying data source.

Namespace:  ESRI.ArcGISExplorer.Mapping

Assembly:  ESRI.ArcGISExplorer (in ESRI.ArcGISExplorer.dll) Version: 2.0.0.1500 (2.0.0.1500)

Syntax

C#
public virtual bool Connect()
Visual Basic (Declaration)
Public Overridable Function Connect As Boolean

Return Value

trueTruetruetrue (True in Visual Basic) if the Layer was connected successfully; otherwise falseFalsefalsefalse (False in Visual Basic)

Remarks

In order for the data to be displayed on a map, the Layer must be in a connected state. Use the Connect() method to establish a connection to this layers underlying data source.

In order to support multiple data formats there are several different Layer types. Each Layer type references a different type of data and has a single property that stores connection details for the data source. The Layer types also provide parameterized constructors that enable you to specify the connection information when you instantiate a Layer. Using the constructors will not implicitly connect the Layer. If you wish to instantiate and connect with a single method call, consider using one of the static Open methods such as FeatureLayer.OpenFileGeodatabaseTable.

Layer TypeData Source PropertyData Source Description
FeatureLayerFeatureLayer.DataSourcePropertiesVector data held in an ESRI shapefile or geodatabase
RasterLayerRasterLayer.DataSourcePropertiesRaster data held in a georeferenced image file or ESRI geodatabase
ServiceLayerServiceLayer.ServiceConnectionPropertiesData provided by a service (ArcGIS Server, ArcIMS, WMS or Bing maps)
GeoRssLayerGeoRssLayer.UrlData provided by a GeoRSS feed
KmlLayerKmlLayer.PathData held in a KML or KMZ file
PackageLayerPackageLayer.PathData held in layer package or layer file
If the connection properties specified are incorrect, the Layer will remain disconnected but no exception will be thrown; use the Layer.ConnectionStatus property to diagnose why the Layer could not be connected.

Examples

The code below illustrates how to populate the data source properties for each derived Layer type and attempts to connect to the underlying data source. If the connection is successful, the Layer is added to a map. If unsuccessful, the connection error is logged.
CopyC#
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

// Import these namespaces to highlight Application and Point name clashes.
using System.Windows.Forms;
using System.Drawing;

// Required additional using statements.
using ESRI.ArcGISExplorer.Data;
using ESRI.ArcGISExplorer.Mapping;

namespace ExamplesCS.Mapping
{
  class LayerConnectionExample
  {
    private void CheckConnectionStatus(Layer layer)
    {
      //Investigate connection issue using the layer connection status property
      ConnectionStatus status = layer.ConnectionStatus;
      if (status.HasError)
      {
        //Print a human-readable error message using the connection status
        System.Diagnostics.Debug.Print("Error message: " + status.ErrorString);

        //If there was an exception print out the call stack
        if(status.ErrorException != null)
          System.Diagnostics.Debug.Print("Call stack: " + status.ErrorException.StackTrace);
      }
    }

    public void Layer_Connections()
    {
      Map map = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Map;

      //FeatureLayer: vector data held in an ESRI shapefile or geodatabase
      //Create a DataSourceProperties object that holds the connection information to the feature class
      DataSourceProperties featureLayerDataSourceProps = new DataSourceProperties(@"C:\Data\Forestry.gdb", "road_hazards");
      //Create a feature layer
      FeatureLayer featureLayer = new FeatureLayer(featureLayerDataSourceProps);
      //Connect the layer to the feature class
      bool isConnected = featureLayer.Connect();

      if (isConnected)
        //Add the layer to the map
        map.ChildItems.Add(featureLayer);
      else
        CheckConnectionStatus(featureLayer);


      //RasterLayer: raster data held in a georeferenced image file or ESRI geodatabase
      //Create a DataSourceProperties object that holds the connection information to the raster data source
      DataSourceProperties rasterLayerDataSourceProps = new DataSourceProperties(@"C:\Data\Scotland.gdb", "NORTHGORMS_HO");
      //Create a raster layer
      RasterLayer rasterLayer = new RasterLayer(rasterLayerDataSourceProps);
      //Connect the layer to the raster data source
      isConnected = rasterLayer.Connect();
      if (isConnected)
        map.ChildItems.Add(rasterLayer);
      else
        CheckConnectionStatus(rasterLayer);


      //ServiceLayer: data provided by a service (ArcGIS Server, ArcIMS, WMS, Virtual Earth)
      //Create a URI object for ArcGIS Online
      Uri serverUri = new Uri("http://services.arcgisonline.com/Server/Services");
      //Define service name for Map Service
      string serviceName = "ESRI_Imagery_World_2D";
      //Create a ServiceConnectionProperties object that holds connection information for the service
      ServiceConnectionProperties serviceLayerConnectionProps = new ServiceConnectionProperties(ServiceType.MapServer, serverUri, serviceName);
      ServiceLayer serviceLayer = new ServiceLayer(serviceLayerConnectionProps);
      isConnected = serviceLayer.Connect();
      if (isConnected)
        map.ChildItems.Add(serviceLayer);
      else
        CheckConnectionStatus(serviceLayer);


      //GeoRssLayer: data provided by a GeoRSS feed
      Uri geoRssUri = new Uri("http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/6697.xml");
      GeoRssLayer geoRssLayer = new GeoRssLayer(geoRssUri);
      isConnected = geoRssLayer.Connect();
      if (isConnected)
        map.ChildItems.Add(geoRssLayer);
      else
        CheckConnectionStatus(geoRssLayer);


      //KmlLayer: data held in a KML file
      string pathToKmlFile = @"C:\Data\KmlExample.kml";
      KmlLayer kmlLayer = new KmlLayer(pathToKmlFile);
      isConnected = kmlLayer.Connect();
      if (isConnected)
        map.ChildItems.Add(kmlLayer);
      else
        CheckConnectionStatus(kmlLayer);


      // PackageLayer: data held in layer package or layer file
      string pathToPackageLayer = @"C:\Data\LpkExample.lpk";
      PackageLayer packageLayer = new PackageLayer(pathToPackageLayer);
      isConnected = packageLayer.Connect();
      if (isConnected)
        map.ChildItems.Add(packageLayer);
      else
        CheckConnectionStatus(packageLayer);
    }

  }



}
CopyVB.NET
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting

' Import these namespaces to highlight Application and Point name clashes.
Imports System.Windows.Forms
Imports System.Drawing

' Required additional using statements.
Imports ESRI.ArcGISExplorer.Data
Imports ESRI.ArcGISExplorer.Mapping


Class LayerConnectionExample
    Private Sub CheckConnectionStatus(ByVal layer As Layer)
        'Investigate connection issue using the layer connection status property
        Dim status As ConnectionStatus = layer.ConnectionStatus
        If status.HasError Then
            'Print a human-readable error message using the connection status
            System.Diagnostics.Debug.Print("Error message: " + status.ErrorString)

            'If there was an exception print out the call stack 
            If status.ErrorException IsNot Nothing Then
                System.Diagnostics.Debug.Print("Call stack: " + status.ErrorException.StackTrace)
            End If
        End If
    End Sub

    Public Sub Layer_Connections()
        Dim map As Map = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Map

        'FeatureLayer: vector data held in an ESRI shapefile or geodatabase
        'Create a DataSourceProperties object that holds the connection information to the feature class
        Dim featureLayerDataSourceProps As New DataSourceProperties("C:\Data\Forestry.gdb", "road_hazards")
        'Create a feature layer
        Dim featureLayer As New FeatureLayer(featureLayerDataSourceProps)
        'Connect the layer to the feature class
        Dim isConnected As Boolean = featureLayer.Connect()

        If isConnected Then
            map.ChildItems.Add(featureLayer)
        Else
            CheckConnectionStatus(featureLayer)
            'Add the layer to the map
        End If


        'RasterLayer: raster data held in a georeferenced image file or ESRI geodatabase
        'Create a DataSourceProperties object that holds the connection information to the raster data source
        Dim rasterLayerDataSourceProps As New DataSourceProperties("C:\Data\Scotland.gdb", "NORTHGORMS_HO")
        'Create a raster layer
        Dim rasterLayer As New RasterLayer(rasterLayerDataSourceProps)
        'Connect the layer to the raster data source
        isConnected = rasterLayer.Connect()
        If isConnected Then
            map.ChildItems.Add(rasterLayer)
        Else
            CheckConnectionStatus(rasterLayer)
        End If


        'ServiceLayer: data provided by a service (ArcGIS Server, ArcIMS, WMS, Virtual Earth)
        'Create a URI object for ArcGIS Online
        Dim serverUri As New Uri("http://services.arcgisonline.com/Server/Services")
        'Define service name for Map Service
        Dim serviceName As String = "ESRI_Imagery_World_2D"
        'Create a ServiceConnectionProperties object that holds connection information for the service
        Dim serviceLayerConnectionProps As New ServiceConnectionProperties(ServiceType.MapServer, serverUri, serviceName)
        Dim serviceLayer As New ServiceLayer(serviceLayerConnectionProps)
        isConnected = serviceLayer.Connect()
        If isConnected Then
            map.ChildItems.Add(serviceLayer)
        Else
            CheckConnectionStatus(serviceLayer)
        End If


        'GeoRssLayer: data provided by a GeoRSS feed
        Dim geoRssUri As New Uri("http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/6697.xml")
        Dim geoRssLayer As New GeoRssLayer(geoRssUri)
        isConnected = geoRssLayer.Connect()
        If isConnected Then
            map.ChildItems.Add(geoRssLayer)
        Else
            CheckConnectionStatus(geoRssLayer)
        End If


        'KmlLayer: data held in a KML file
        Dim pathToKmlFile As String = "C:\Data\KmlExample.kml"
        Dim kmlLayer As New KmlLayer(pathToKmlFile)
        isConnected = kmlLayer.Connect()
        If isConnected Then
            map.ChildItems.Add(kmlLayer)
        Else
            CheckConnectionStatus(kmlLayer)
        End If


        ' PackageLayer: data held in layer package or layer file
        Dim pathToPackageLayer As String = "C:\Data\LpkExample.lpk"
        Dim packageLayer As New PackageLayer(pathToPackageLayer)
        isConnected = packageLayer.Connect()
        If isConnected Then
            map.ChildItems.Add(packageLayer)
        Else
            CheckConnectionStatus(packageLayer)
        End If
    End Sub

End Class

See Also