1

Recently this community helped me to get a list of the tables from an MXD file:

IMapDocument mapDoc = new MapDocument();
mapDoc.Open("C://mymxd.mxd");

IActiveView act = mapDoc.ActiveView;
IMap map = act.FocusMap;

IStandaloneTableCollection coleccionTablas = (IStandaloneTableCollection)map;

for (int i = 0; i < coleccionTablas.StandaloneTableCount; i++)
{
    IStandaloneTable stTable = coleccionTablas.get_StandaloneTable(i);
    string nameTOC = stTable.Name;
}

That way I get the name of the table as shown in the "Table of Contents" of ArcMap. I thought that from there, it would be easy to get the name of the table as stored in the database. In other words: I need the datasource of the table.

I found this code: Changing data source of table using ArcObjects?

That same solution is the only one I have found so far in different sites. It seems pretty easy, but I only get ITable objects that are null... so getting the datasource for those is impossible.

This is my code:

for (int i = 0; i < coleccionTablas.StandaloneTableCount; i++)
{
    IStandaloneTable stTable = coleccionTablas.get_StandaloneTable(i);
    string nameTOC = stTable.Name;

    ITable table = stTable.Table; //--> table is always null!!
    IDataset dataset = (IDataset)table;
    IDatasetName datasetName = (IDatasetName)dataset.FullName;
}

I have tested several MXD files with tables, but the ITable object I get from the IStandaloneTable is always null and I have no idea why.

I have also tried with ITableCollection, but the result is the same.

Does anyone know why I keep getting nulls? Maybe there is something horribly wrong with my code.

5
  • 1
    When you set nameTOC is that actually setting to the name of the table in TOC? Also your first bit of code is accessing a MapDocument object, are you actually wanting to get the tables in the currently opened document, in that case you want to be accessing the map from mxdocument.
    – Hornbydd
    Commented Jun 29, 2018 at 14:18
  • 1
    Strange ... did you try casting stTable to IDataLayer2 then calling IDataLayer2.Connect ? (Maybe for performance MapDocument does lazy loading of standalone tables since MapControl can't display them anyway). Commented Jun 29, 2018 at 17:11
  • Also, you can try casting datasetName to IName and then call IName.Open(), which should return an object that you can cast to ITable. Commented Jun 29, 2018 at 17:16
  • 1
    Is your table valid? (stTable.Valid) Commented Jul 2, 2018 at 4:03
  • @MichaelStimson if the datasource is not broken, yes the table is valid. With broken datasources I get a false value.
    – O.D.
    Commented Jul 2, 2018 at 6:11

1 Answer 1

2

Thanks to your comments, I've been able to get the datasource of my tables.

This morning I have realized my code had been tested against tables with a broken datasource. I have created an MXD with a table without a broken datasource and the code I posted in my question works without any problem.

But I also need to get the datasource of a table if it's broken. As suggested, I've tried to cast it to IDataLayer2, but I haven't been able to get the datasource (I guess I'm doing something wrong).

Instead of casting it to IDataLayer2, I have casted it to IDataLayer.

IStandaloneTable stTable = coleccionTablas.get_StandaloneTable(i);

var dataLayer = (IDataLayer)stTable;
var datasetName = (IDatasetName)dataLayer.DataSourceName;
string name = datasetName.Name;

That works for broken and unbroken datasources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.