Using Layer.ConnectionStatus

2094
17
Jump to solution
12-04-2020 06:54 AM
BrianBulla
Occasional Contributor III

Hi,

I'm using the following code to determine which layers in my project might have a missing data source, so that my code doesn't crash later and I can present the user with a message to fix the problem.  But when I call Layer.ConnectionStatus I get a 'NullReference' exception.  Which I guess makes sense, since the layer is missing the data source.  Am I just not using the ConnectionStatus properly?? 

 foreach (Layer layer in map.GetLayersAsFlattenedList().OfType<FeatureLayer>())
                    {
                        if (layer.ConnectionStatus == ConnectionStatus.Broken)
                        {
                            ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SOME KIND OF ERROR MESSSAGE");
                        }
0 Kudos
17 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Brian,

 I can't see anything wrong (except the continue statement i added under the 'broken' condition), but let's try to replace one segment in your code with this:

//Check for any Joins on layers with a selected feature
Boolean joinExists = false;
Boolean brokenExists = false;
var map = MapView.Active.Map;
foreach (FeatureLayer currentLayer in map.GetLayersAsFlattenedList().OfType<FeatureLayer>())
{
		//***** where my original code get the NullReference exception
		if (currentLayer.ConnectionStatus == ConnectionStatus.Broken)
		{
				ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(currentLayer.Name.ToString() + " has a missing data source.  Please remove or fix this layer before continuing.", "Missing Data Source");
				brokenExists = true;
                                // added this to prevent exceptions in the remaing loop code
				continue;
		}

		if ((currentLayer.GetTable().IsJoinedTable() == true) && (currentLayer.SelectionCount > 0))
    {
        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(currentLayer.Name.ToString() + " has a joined table.  Please remove any joins before continuing.", "Joined table detected");
        joinExists = true;
    }
}

Let's see if this is causing the NULL exception as well.

0 Kudos
BrianBulla
Occasional Contributor III

Hi Wolf,

Well I still get the NullReference exception, but at a different line of code.....see below.

Basically with this tool, I was trying to add a way to detect a 'broken' layer.  As the code was, it would always crash when a broken layer was in the Map, so I just wanted to notify the user to fix the layer before proceeding.  I guess I could just throw an Exception handler in there.  I'm not quite clear on what is happening here.

BrianBulla_0-1607520967667.png

 

 

0 Kudos
BrianBulla
Occasional Contributor III

And just to add to my comment above, if I remove the 'broken' layer from the Map and run the tool, all is good.  But then I will 'Undo' to get the broken layer back in the Map and run the tool, then it fails again with the NullReference exception.

 

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Brian,

 This latest exception makes no sense ... a boolean local variable shouldn't be able to throw a null reference exception.  One possibility would be that the code causing the exception is not in sync with your source code.  So I would suggest at this time to make sure that we are in sync by closing all instances of ArcGIS Pro (even use Task Manager to make sure no 'hidden' Pro instance is running), then deleting all addins (by deleting everything in the ...\Documents\ArcGIS\AddIns folder), then rebuilding your add-in and checking for the exception.

0 Kudos
BrianBulla
Occasional Contributor III

Hi Wolf,

OK, so things seem to be working better now.   Since most of these tools I create are shared, I actually have them all sitting on a shared folder on the network.  All the users connect to the folder to bring in the tools to ArcGIS Pro.

Correct me if I am wrong, but if I update the "Version" in the config.daml when I am updating the tools on my PC, when I go to Build/Debug the project won't ArcGIS Pro see the updated version sitting in my AddIns folder and load up that one and NOT the one sitting on the network (with the older version #)??

Otherwise, the .ConnectionStatus does seem to be working with the new code you sent me.  The only difference I see is using FeatureLayer instead of BasicFeatureLayer.  Is that the issue??

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Brian,

 That was a tricky issue to solve... as far as the loading sequence you can find information here: https://github.com/Esri/arcgis-pro-sdk/wiki/ProConcepts-Advanced-Topics#2-admin-well-known-folders  Looking at this document i think the network share (in the registry) will trump everything else.  Pro is using the following sequence: network path (registry) … user defined paths … and finally the default path for add-ins.  The version tag in the config daml is actually only used to prevent an add-in from being loaded and run on an older version of Pro. 

As far as the 'down cast' from FeatureLayer to BasicFeatureLayer, the only impact here is that FeatureLayers (you are getting FeatureLayers from the layer list) has more functionality (via properties and methods) than  BasicFeatureLayer from which it is derived from.

2020-12-09_8-52-41.png

Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Brian, 

 I have to correct my previous reply as this statement was wrong:

 The version tag in the config daml is actually only used to prevent an add-in from being loaded and run on an older version of Pro. 

The desktopVersion tag (see below) is actually used to prevent an add-in from being loaded and run on an older version of Pro.  The version tag (1.25 in the sample snippet below) dictates which add-in (with the same "AddinInfo id") will be loaded by ArcGIS Pro as Pro will always load the add-in with highest version value.  So for example you publish version=1.01 on your network share where everybody's Pro can use that add-in, you can then increment the version to version=1.02 on your development machine and now your machine will always load the 1.02 version.  

<AddInInfo id="{ba4449fa-76b4-4d5a-9d07-0e0162aeeb3a}" version="1.25" desktopVersion="2.5.0">
...
</AddInInfo>

 

BrianBulla
Occasional Contributor III

Thanks Wolf!

0 Kudos