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

IMapDocument mapDoc = new MapDocument();

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.

  • 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 Jun 29 '18 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). – Kirk Kuykendall Jun 29 '18 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. – Kirk Kuykendall Jun 29 '18 at 17:16
  • 1
    Is your table valid? (stTable.Valid) – Michael Stimson Jul 2 '18 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. Jul 2 '18 at 6:11

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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