Select to view content in your preferred language

Unable to cast COM object of type 'System.__ComObject' (to IStandAloneTable)

3545
2
06-30-2010 01:58 AM
PieterLinks
Occasional Contributor
Hi,

When I try to add a table from a MDB to a MapControl's StandAloneTableCollection, I get an error. What am I doing wrong? The table exists and is not empty.

{System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Carto.IStandaloneTable'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{FFC6B179-E3EC-11D3-A096-00C04F6BC626}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
   at <snip>.AddTableToMapControl() in <snip><snip>}


public void AddTableToMapControl()
{
  IMapControl3 pMapControl = (IMapControl3)this.AxMapControl1.Object;
  IWorkspaceFactory accessWorkspaceFactory = new AccessWorkspaceFactoryClass();
  IFeatureWorkspace featureWorkspace;
      ITable geodatabaseTable;
      IStandaloneTable geodatabaseStTable;
      IStandaloneTableCollection pStTableCollection = pMapControl as IStandaloneTableCollection;
   
      try
      {
            featureWorkspace = accessWorkspaceFactory.OpenFromFile("c:/somewhere/somedb.mdb", 0) as IFeatureWorkspace;
            geodatabaseTable = featureWorkspace.OpenTable("SomeTable");
            geodatabaseStTable = (IStandaloneTable)geodatabaseTable; <----- here it fails with the error
            pStTableCollection.AddStandaloneTable(geodatabaseStTable);
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.ToString());
  }
}


Many thanks,
0 Kudos
2 Replies
NeilClemmons
Honored Contributor
The Table coclass does not implement IStandAloneTable, which is why you're getting the error.  To add a standalone table to the map, you have to create a new one and set its Table property.  It's basically the same thing you do when you want to add a feature layer - create a new feature layer and set its FeatureClass property.
0 Kudos
PieterLinks
Occasional Contributor
Thanks Neil for your answer.

I did as you wrote: created a new StandAloneTable, set its Table property to geodatabaseTable and tried to add it to the collection, but there it fails.

Never mind, I made a class which contains the ITable and a Name property, and put that in an ICollection - an ITableCollection doesn't have a Name property.
0 Kudos