[GIS] How to set the workspace to a featureclass from an unknown file gdb

arcobjects

When a user loads an mxd, I am getting the IWorkspace object from the featurelayer's dataset:

IFeatureClass featClass = (IFeatureClass)featLayer.FeatureClass;
IDataset dataSet = (IDataset)featClass.FeatureDataset;
IWorkspace workSpace = (IWorkspace)dataSet.Workspace;

But this obviously does not work if the user has a layer from a file geodatabase (dataset is null).

I can check to see if it's a gdb layer but nothing in the featurelayer or its featureclass tells me the source of the gdb.

How do I go about setting the workspace object to a featurelayer that comes from a gdb?

Best Answer

Change the line

IDataset dataSet = (IDataset)featClass.FeatureDataset;

to

IDataset dataset = (IDataset)featClass;