couldn't create instance on siteprovider page

Last post 06-04-2008 10:20 AM by mohdkidwai@hotmail.com. 4 replies.

Sort Posts:

  • couldn't create instance on siteprovider page

    05-21-2008, 10:02 AM
    • Loading...
    • amy20
    • Joined on 05-21-2008, 1:54 PM
    • Posts 2

     Hi,

     One siteprovder.vb

    Public Shared ReadOnly Property Articles() As ArticlesProvider

    Get

    Return ArticlesProvider.Instance

    End Get

    End Property

    I got "Value cannot be null. Parameter name: type" message.

    at System.Activator.CreateInstance(Type type, Boolean nonPublic)

    at System.Activator.CreateInstance(Type type)

     

    Any idea?

    Thanks

    Amy

    Filed under:
  • Re: couldn't create instance on siteprovider page

    05-21-2008, 11:52 AM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 923

    Take a look at the ConfigSection file.

    You need to make sure you have a ProviderType defined in the ArticlesElement class, and that it has a value. The code should define a default value of MB.TheBeerHouse.DAL.SqlClient.SqlArticlesProvider.
     

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: couldn't create instance on siteprovider page

    05-21-2008, 1:24 PM
    • Loading...
    • amy20
    • Joined on 05-21-2008, 1:54 PM
    • Posts 2

    Thanks for your response.

    I have defined it in  ConfigSection.vb. The error is on ArticlesProvider.vb page now. I cann't create a instance. on the watch, the value of instance() is {"Value cannot be null. Parameter name: type"}, but Type is "MB.TheBeerHouse.DAL.ArticlesProvider".

    Could you shed a light?

    Thanks again.

    ------------------------------------------------------------------------------- 

    <ConfigurationProperty(
    "providerType", DefaultValue:="MB.TheBeerHouse.DAL.SqlClient.SqlArticlesProvider")> _

    Public Property ProviderType() As String

    Get

    Return CStr(Me("providerType"))

    End Get

    Set(ByVal value As String)

    Me("providerType") = value

    End Set

    End Property

    ---------------------------------------------------------------

     

    ----------------------------------------------------------------------------------------------------- 

    Private Shared _instance As ArticlesProvider = Nothing

    Public Shared ReadOnly Property Instance() As ArticlesProvider

    Get

    If IsNothing(_instance) Then

    _instance = CType(Activator.CreateInstance( Type.GetType(Globals.Settings.Articles.ProviderType)), ArticlesProvider)

    End If

    Return _instance

    End Get

    End Property

    ----------------------------------------------------

     

    Filed under:
  • Re: couldn't create instance on siteprovider page

    05-21-2008, 3:57 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 2:51 PM
    • Decatur, IL USA
    • Posts 923

     This is basically how this works:

    When you attempt to return the value of Instance, it gets it by using the System.Activator CreateInstance method, and casting the returned instance to an ArticlesProvider. The CreateInstance method takes a Type, and returns an instance of that type. It gets the Type from the Type.GetType method, which takes a typeName string to return the Type you want. 

    I know this sounds all complicated, but it really just boils down to the value of Globals.Settings.Articles.ProviderType. If that is null (Nothing), then it causes a chain reaction. It causes Type.GetType to be null, which causes Activator.CreateInstance to be null, which causes the cast to be null, which causes _instance to be null, which causes ArticlesProvider.Instance() to return a null value.

    So, you have to make absolutely sure that Globals.Settings.Articles.ProviderType isn't returning null (Nothing). That can be caused by not defining it in the ArticlesElement, not having a proper theBeerHouse section in your web.config, or by not having the right Settings property defined in your Globals class.

    What I would do is to set a debug breakpoint in SiteProvder.vb, right on the line where it tries to get ArticlesProvider.Instance, and step through using F11 from that point forward. You should be able to see exactly where this blows up that way. 

     

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: Getting a configuration error message

    06-04-2008, 10:20 AM

    I am sorry. I am quite new here. I am trying to practice the beerhouse starter kit so for that reason I am trying to create only Articles module. However the custom configuration class is quite complex and

    I cannot fix this configuration error I am getting. I understand the flow as I read your previous posts but I am not able to work around with the connectionstring through the custom configuration class

    I have created the following connection by use a gridview control just  cheated a  bit.  

    <section name="test" type= "DALCM.Testone, __code" /> 

    ....

    ....

    <test defaultConnectionStringName="LocalServer">
        <articles pageSize="10"/>
      </test>

    <connectionStrings>
        <remove name="LocalServer"/>
        <add name="LocalServer" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\LGateContentMgt.mdf&quot;;Integrated Security=True;Connect Timeout=30;User Instance=True"
          providerName="System.Data.SqlClient" />
      </connectionStrings>
     

     so in my configuration section I am using

    [ConfigurationProperty("defaultConnectionStringName", DefaultValue = "LocalServer")]
            public string DefaultConnectionStringName
            {
                get { return (string)base["defaultConnectionStringName"]; }
                set { base["connectionStdefaultConnectionStringNameringName"] = value; }

            }
    public class ArticlesElement : ConfigurationElement
            {
                [ConfigurationProperty("connectionStringName")]
                public string ConnectionStringName
                {
                    get { return (string)base["connectionStringName"]; }
                    set { base["connectionStringName"] = value; }
                }
                public string ConnectionString
                {
                    get
                    {
                       
                        string connStringName = (string.IsNullOrEmpty(this.ConnectionStringName) ?
                           Globals.settings.DefaultConnectionStringName : this.ConnectionStringName); // I am getting error on this line since it is returning null value
                        return WebConfigurationManager.ConnectionStrings[connStringName].ConnectionString;
                    }
                }

     

    I do not understand why it is returning null value here.

     I am using teh following Providertype

    [ConfigurationProperty("providerType", DefaultValue = "SqlArticlesProvider.System.Data.SqlClient")]
                public string ProviderType
                {
                    get { return (string)base["providerType"]; }
                    set { base["providerType"] = value; }
                }
     

    Please guide me so atleast I should be able to see the connectionstring in my label control that I have created. 

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter