locked
Unable to cast object of type <xxx> to type <xxx> RRS feed

  • Question

  • User-700129015 posted

    I'm working on an application that uses a custom configuration file and reads the contents into a public shared readonly variable named "settings":

    Public   NotInheritable Class Globals

    Public Shared ReadOnly Settings As TheBMTStoreSection = _

    CType(WebConfigurationManager.GetSection("TheBMTStore"), TheBMTStoreSection)Public Shared ThemesSelectorID As String = ""

     End

    Class

     This part seems to work just fine.  The problem I can't seem to solve is when I try to access the "Settings" from a class that provides connection info.  Here is the code that creates an instance of the "Settings" to get at the "ProviderType":

     

    Private Shared _instance As StoreProvider

    Public Shared ReadOnly Property Instance() As StoreProviderGet

     If IsNothing(_instance) Then

     _instance = CType(Activator.CreateInstance( _

    Type.GetType(BMTStore.Globals.Settings.Store.ProviderType)), StoreProvider)

    End If

    Return  _instance

    End Get

    End Property

    The application throws an "unable to cast object of type 'TheBMTStoreSection' to type 'BMTStore.TheBMTStoreSection'.

    The web.config contains the following section name and section:

    <section name="TheBMTStore" type="TheBMTStoreSection"/>

    <TheBMTStore defaultConnectionStringName="connBMT" defaultCacheDuration="600" >

    <contactForm mailTo="mysite@mysite.com"/>

    <store businessEmail="mysite@mysite.com"/>

    </TheBMTStore>

    Any help  would be greatly appreciated.

    Tuesday, February 9, 2010 1:38 PM

All replies

  • User-1528148979 posted

    Did you try ?


        Public Shared ReadOnly Settings As BMTStore.TheBMTStoreSection = DirectCast(WebConfigurationManager.GetSection("TheBMTStore"), BMTStore.TheBMTStoreSection)
        Public Shared ThemesSelectorID As String = ""
    


    Tuesday, February 9, 2010 2:25 PM
  • User-700129015 posted

     I tried the above and received the same error message.  Here is the stack trace from the error:

    [InvalidCastException: Unable to cast object of type 'TheBMTStoreSection' to type 'BMTStore.TheBMTStoreSection'.]
       BMTStore.Globals..cctor() in C:\Visual Studio 2008\WebSites\BMTStore\BMTStore\App_Code\Globals.vb:5

    [TypeInitializationException: The type initializer for 'BMTStore.Globals' threw an exception.]
       BMTStore.StoreProvider.get_Instance() in C:\Visual Studio 2008\WebSites\BMTStore\BMTStore\Classes\StoreProvider.vb:13
       BMTStore.SiteProvider.get_Store() in C:\Visual Studio 2008\WebSites\BMTStore\BMTStore\Classes\SiteProvider.vb:8
       BMTStore.Department.GetDepartments() in C:\Visual Studio 2008\WebSites\BMTStore\BMTStore\Classes\Department.vb:102

    [TargetInvocationException: Exception has been thrown by the target of an invocation.]
       System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0
       System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +71
       System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +350
       System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
       System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +488
       System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1247
       System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +95
       System.Web.UI.WebControls.ListControl.PerformSelect() +34
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
       System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
       System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +18
       System.Web.UI.Control.PreRenderRecursiveInternal() +80
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

    Tuesday, February 9, 2010 2:38 PM