Error when trying to create a FeatureClass based upon a Query Layer

Discussion created by sapaplanus on May 5, 2012
I am receiving an error : Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Geodatabase.IFeatureClass'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{D4803EE6-79F4-11D0-97FC-0080C7F79481}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

when I execute: " IFeatureClass queryFClass = (IFeatureClass)queryClass; "

I am following the sample code I found for creating a query layer that is extracting data from an Sql Server 2008 spatial database that I want to add to the ESRI AxMapControl.   I am not sure why it is failing, and any suggestions would be helpful.  

Here is the code:

// Create the SQL workspace factory
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");
            IWorkspaceFactory wsFact = (IWorkspaceFactory)Activator.CreateInstance(factoryType);

            // Set the connection properties
            IPropertySet connectionProps = new PropertySet();
            connectionProps.SetProperty("dbclient", "SQLServer");
            connectionProps.SetProperty("serverinstance", "rixen");
            connectionProps.SetProperty("authentication_mode", "OSA");
            connectionProps.SetProperty("database", "Test_iBet");
            string s1 = string.Empty;
            // Open the workspace
            IWorkspace ws = wsFact.Open(connectionProps, 0);
            ISqlWorkspace sqlWS = (ISqlWorkspace)ws;
                // Create a QueryDescription to get the spatial data
                IQueryDescription queryDescription = sqlWS.GetQueryDescription("select * from FY2012Activities");
                queryDescription.OIDFields = "PID";

                IStringArray columnNames = null;
                IStringArray columnTypes = null;
                IVariantArray areNullable = null;
                ILongArray columnSizes = null;
                ILongArray columnPrecisions = null;
                ILongArray columnScales = null;
                sqlWS.GetColumns("FY2012Activities", out columnNames, out columnTypes, out areNullable,
                    out columnSizes, out columnPrecisions, out columnScales);

                // The arrays are all the same size, so this is safe, and as a sanity check.  This does return all the columns from FY2012Activites table.
                for (int i = 0; i < columnNames.Count; i++)
                    Console.WriteLine("Name: {0}", columnNames.get_Element(i));
                    Console.WriteLine("Type: {0}", columnTypes.get_Element(i));
                    Console.WriteLine("Is Nullable: {0}", areNullable.get_Element(i));
                    Console.WriteLine("Size: {0}", columnSizes.get_Element(i));
                    Console.WriteLine("Precision: {0}", columnPrecisions.get_Element(i));
                    Console.WriteLine("Scale: {0}", columnScales.get_Element(i));
                string newName = string.Empty;
                ITable queryClass = sqlWS.OpenQueryClass(newName, queryDescription);
                IFeatureClass queryFClass = (IFeatureClass)queryClass; 

                // Get a feature layer from the feature class
                //IFeatureClass fCls = queryClass as IFeatureClass;
                IFeatureLayer fLyr = new FeatureLayer();
                fLyr.FeatureClass = queryFClass;

                // Add the layer to the map control
            catch (Exception ex)