Add a Table from a Geodatabase

1601
2
Jump to solution
10-09-2019 06:05 AM
EmmanuelJohn
New Contributor II

Hello everyone,

protected override void OnClick()
        {
            Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"\\dep-gisdev\tidelandsdata\tlprod\Tidelands_Publication.gdb")));
            Table table = fileGeodatabase.OpenDataset<Table>("Composite_Table");
        }

I'm having trouble with C# code for ArcGIS Pro which will add a Table stored on a local Geodatabase in the Table of Contents for ArcGIS Pro. i was able to create some part of the code, but i'm getting an Exception Unhanded Error from Visual Studio stating : ArcGIS.Core.ConstructedOnWrongThreadException: 'This object must be created within the lambda passed to QueuedTask.Run, or on a compatible Sta thread'. I'm using a simple button to execute this code OnClick. 

0 Kudos
1 Solution

Accepted Solutions
RichRuh
Esri Regular Contributor

Hi Manny,

The short answer is that you need to call geodatabase routines from a special thread using the QueuedTask class.  Your code will look like something like this:

await QueuedTask.Run( () =>
{
  Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"\\dep-gisdev\tidelandsdata\tlprod\Tidelands_Publication.gdb")));
  Table table = fileGeodatabase.OpenDataset<Table>("Composite_Table");
}‍‍‍‍‍);

More details on the Pro threading model can be found in the topic Working with Multithreading in ArcGIS Pro.

Some other comments:

  • Make you sure add memory management to your code, to properly release the Geodatabase and Table objects when you are finished using them.  Details for how to do this can be found here.
  • Once you have opened your table using the code above, you still need to add a StandAloneTable to your Map.  This can be accomplished as follows:

IStandaloneTableFactory tableFactory = StandaloneTableFactory.Instance;
tableFactory.CreateStandaloneTable(table, MapView.Active.Map);

Again, this needs to be wrapped inside a call to QueuedTask.Run().

I hope this helps,

--Rich

View solution in original post

2 Replies
RichRuh
Esri Regular Contributor

Hi Manny,

The short answer is that you need to call geodatabase routines from a special thread using the QueuedTask class.  Your code will look like something like this:

await QueuedTask.Run( () =>
{
  Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"\\dep-gisdev\tidelandsdata\tlprod\Tidelands_Publication.gdb")));
  Table table = fileGeodatabase.OpenDataset<Table>("Composite_Table");
}‍‍‍‍‍);

More details on the Pro threading model can be found in the topic Working with Multithreading in ArcGIS Pro.

Some other comments:

  • Make you sure add memory management to your code, to properly release the Geodatabase and Table objects when you are finished using them.  Details for how to do this can be found here.
  • Once you have opened your table using the code above, you still need to add a StandAloneTable to your Map.  This can be accomplished as follows:

IStandaloneTableFactory tableFactory = StandaloneTableFactory.Instance;
tableFactory.CreateStandaloneTable(table, MapView.Active.Map);

Again, this needs to be wrapped inside a call to QueuedTask.Run().

I hope this helps,

--Rich

EmmanuelJohn
New Contributor II

Thank You Rich, 

i greatly appreciate your help, along with the proper resources. i was able to change the code to perform the function i needed using your code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace AddTable
{
    internal class Button1 : Button
    {
        protected override async void OnClick()
        {
            await QueuedTask.Run(() =>
            {
                Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"\\dep-gisdev\tidelandsdata\tlprod\Tidelands_Publication.gdb")));
                Table table = fileGeodatabase.OpenDataset<Table>("TL_Activities");
                IStandaloneTableFactory tableFactory = StandaloneTableFactory.Instance;
                tableFactory.CreateStandaloneTable(table, MapView.Active.Map);
            });
        }
    }
}