Welcome to the CodeSmith Community!

DataRepository not finding provider. says object reference not set.

.netTiers

A description has not yet been added to this group.

DataRepository not finding provider. says object reference not set.

  • rated by 0 users
  • This post has 18 Replies |
  • 3 Followers
  • I am getting an error saying "Object reference not set to an instance of an object". 

    Apparently the problem is that it breaks in the foreach loop after finding NetTiersServiceSection, but when it returns the get { return (ProviderSettingsCollection)base["providers"];  from NetTiersServiceSection class, the provider count is = 0.  I don't know how this part really works, but it seem to be the problem. 

    Here is the error:

    Server Error in '/WareHouse2' Application.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 74:                             _providers = new NetTiersProviderCollection();
    Line 75: 
    Line 76:                             ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(NetTiersProvider));
    Line 77:                             _provider = _providers[section.DefaultProvider];
    Line 78: 


    Source File: c:\C_WH\WareHouse2\App_Code\DAL\NetTier\NetTier.DAL\DataRepository.cs    Line: 76

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       NetTier.DAL.DataRepository.LoadProviders() in c:\C_WH\WareHouse2\App_Code\DAL\NetTier\NetTier.DAL\DataRepository.cs:76
       NetTier.DAL.DataRepository.get_ProductsProvider() in c:\C_WH\WareHouse2\App_Code\DAL\NetTier\NetTier.DAL\DataRepository.cs:302
       WareHouse.DAL.DataObjects.SqlServer.SqlServerProductDao.GetProducts() in c:\C_WH\WareHouse2\App_Code\DAL\DataObjects\SqlServer\SqlServerProductDao.cs:58
       WareHouse.BLL.Facade.ProductFacade.GetProducts() in c:\C_WH\WareHouse2\App_Code\BLL\Facade\ProductFacade.cs:32
    
    [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) +72
       System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +296
       System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
       System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +17
       System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +676
       System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +2664
       System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +84
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
       C1.Web.C1WebGrid.C1WebGrid.DataBind(Boolean keepGroups) +280
       C1.Web.C1WebGrid.C1WebGrid.DataBind() +39
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +92
       System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +100
       System.Web.UI.Control.EnsureChildControls() +134
       System.Web.UI.Control.PreRenderRecursiveInternal() +109
       System.Web.UI.Control.PreRenderRecursiveInternal() +233
       System.Web.UI.Control.PreRenderRecursiveInternal() +233
       System.Web.UI.Control.PreRenderRecursiveInternal() +233
       System.Web.UI.Control.PreRenderRecursiveInternal() +233
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4437
    
     

     

    ============================================================================
    Here is the the code for DataRepository:

    if (_provider == null)

    {

          #region Find NetTiersServiceSection

         

        // Get a reference to the <netTiersService> section

        //NetTiersServiceSection section = (NetTiersServiceSection)WebConfigurationManager.GetSection("netTiersService");

          Configuration config = null;

     

          // load specific config file

          if ( HttpContext.Current != null )

          {

                config = WebConfigurationManager.OpenWebConfiguration("~");

          }

          else

          {

                string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Replace(".config", "").Replace(".temp", "");

                config = ConfigurationManager.OpenExeConfiguration(configFile);

          }

     

          // find the specific NetTiersServiceSection for this assembly

              foreach ( ConfigurationSection temp in config.Sections )

              {

                    if ( temp is NetTiersServiceSection )

                    {

                          section = temp as NetTiersServiceSection;

                          break;

                    }

              }

               

              #endregion Find NetTiersServiceSection

     

            // Load registered providers and point _provider to the default provider

            _providers = new NetTiersProviderCollection();

     

            ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(NetTiersProvider));

            _provider = _providers[section.DefaultProvider];

     

        if (_provider == null)

        {

            throw new ProviderException("Unable to load default NetTiersProvider");

        }

    }

     

    ========================================================================

    This is the NetTierServiceSection that should be picking up the provider

     

        public class NetTiersServiceSection : ConfigurationSection

        {

            /// <summary>

            /// Gets the providers.

            /// </summary>

            /// <value>The providers.</value>

            [ConfigurationProperty("providers")]

            public ProviderSettingsCollection Providers

            {

                get { return (ProviderSettingsCollection)base["providers"]; }

            }

     

            /// <summary>

            /// Gets or sets the default provider.

            /// </summary>

            /// <value>The default provider.</value>

            [StringValidator(MinLength = 1)]

            [ConfigurationProperty("defaultProvider", DefaultValue = "SqlNetTiersProvider")]

            public string DefaultProvider

            {

                get { return (string)base["defaultProvider"]; }

                set { base["defaultProvider"] = value; }

            }

        }

     

    ==================================================================================

    Here is the code for  my web.config which has the NetTierSection:

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

    <configSections>

        <section name="netTiersService" type="NetTier.DAL.Bases.NetTiersServiceSection, NetTier.DAL"/>

      </configSections>

      <connectionStrings>

        <remove name="aspnetdbConnectionString"/>

        <add name="aspnetdbConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>

        <add name="WareHouseConnectionString" connectionString="Data Source=PROB001\SQL2005TEST;Initial Catalog=WareHouse;Integrated Security=True" providerName="System.Data.SqlClient"/>

      </connectionStrings>

      <appSettings/>

      <system.web>

        <membership defaultProvider="LocalMembershipProvider">

          <providers>

            <add connectionStringName="aspnetdbConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" name="LocalMembershipProvider" type="System.Web.Security.SqlMembershipProvider"/>

          </providers>

        </membership>

    </system.web>

      <netTiersService defaultProvider="SqlNetTiersProvider">

        <providers>

          <add name="SqlNetTiersProvider" type="NetTier.DAL.SqlClient.SqlNetTiersProvider, NetTier.DAL.SqlClient"

               useStoredProcedure="true" connectionStringName="WareHouseConnectionString" applicationName="WareHouseWeb"  providerInvariantName="System.Data.SqlClient"/>

        </providers>

      </netTiersService>

    </configuration>

    Has anyone seen this problem or see something obvious.  This has been driving me Nuts for about 3 days now.

    Thanks

    Nick

  • Nick,

    Check IIS Manager and make sure that your virtual IIS directory is setup as an application (Under Directory tab there is a section called Application Settings)

    Thanks,
    Michael

    nickpatel wrote:
    I am getting an error saying "Object reference not set to an instance of an object". 

    Apparently the problem is that it breaks in the foreach loop after finding NetTiersServiceSection, but when it returns the get { return (ProviderSettingsCollection)base["providers"];  from NetTiersServiceSection class, the provider count is = 0.  I don't know how this part really works, but it seem to be the problem. 

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

  • Thanks for anwering back.  I did check and it is under application settings.   Infact, I even deleted the virtual directory from IIS and then recreated the Vitural Directory, but it still has the same message.

     

    thanks

    Nick

  • Have you tried debugging, and setting a break point in NetTiersServiceSection discovery?  See if it's actually coming back null.
    //breakpoint here
    if (HttpContext.Current!= null)
        config = WebConfigurationManager.OpenWebConfiguration("~); // should not be null
    then...

    Breakpoint here:
    if (temp is NetTiersServiceSection) -- If none of the temp vars meet this condition then we have another problem on our hands.

    Double check your namespaces in the web.config in case you've changed them
    ex - NetTier.DAL.Bases.NetTiersServiceSection, NetTier.DAL 
    might be NetTiers.DAL or something.


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------

  • I did the break points and been through the code unable to understand why its behaving this way.  Here is what each break points yielded

    ================================================================

    config = WebConfigurationManager.OpenWebConfiguration("~);

    showed following values:

                config = {System.Configuration.Configuration}

    ================================================================

    When I put break point below, it went to NetTier.DAL.Bases.NetTiersServiceSection returned ProviderSettingsCollection value as show (By the way, it only enters NetTier.DAL.Bases.NetTiersServiceSection in the NetTiersServiceSection.cs class once after I have opened VS2005 otherwise it won't even go there when I restart the debugger. If I open and close VS2005 it will enter it).  The Count for (ProviderSettingsCollection)base["providers"] was 0

    if (temp is NetTiersServiceSection)

       ProviderSettingsCollection = System.Configuration.ProviderSettingsCollection //value

    ================================================================

    In the if (temp is NetTiersServiceSection) it never hit the code that says section = temp as NetTierServiceSection

    if ( temp is NetTiersServiceSection )

    {

            section = temp as NetTiersServiceSection;           // it never hits this part of the code

           break;

    }

    so my section = null

    ================================================================

    The _Provider seems to show _providers = {NetTier.DAL.Bases.NetTiersProviderCollection}

    The part where I checked the web.config and it is NetTier.DAL. 

     

    thanks
    Nick

  • If it can't cast it as a NetTiersServiceSection then there's got to be something wrong with the web.config somewhere.  Because it is finding the config file, it just can't cast it back.  You double checked the namespaces as well for the NetTiersServiceSection?  And

    What version of the templates are you using by the way?


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------

  • iis.jpg

    Nick,

    What i meant is to verify if you have application name set for your virtual directory (looks like following  line of code can't pick up the web.config at the root of the project WebConfigurationManager.OpenWebConfiguration("~");)

    See the attach image where the application name is set

    Thanks,
    Michael

    nickpatel wrote:

    Thanks for anwering back.  I did check and it is under application settings.   Infact, I even deleted the virtual directory from IIS and then recreated the Vitural Directory, but it still has the same message.

     

    thanks

    Nick


    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

  • Hi Robert,

    Could it be that the config that is found is located in the different location (rather then project root location)?

    Michael

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

  • Well, in webconfig I have

    <configSections>

    <section name="netTiersService" type="NetTier.DAL.Bases.NetTiersServiceSection, NetTier.DAL"/>

    </configSections>

     

    and

    <netTiersService defaultProvider="SqlNetTiersProvider">

    <providers>

    <add name="SqlNetTiersProvider" type="NetTier.DAL.SqlClient.SqlNetTiersProvider, NetTier.DAL.SqlClient"

    useStoredProcedure="true" connectionStringName="WareHouseConnectionString" applicationName="WareHouseWeb" providerInvariantName="System.Data.SqlClient"/>

    </providers>

    </netTiersService>

    The type in NetTiersService starts with NetTier.DAL

    The project explorer shows: NetTier.DAL  and NetTier.DAL.sqlClient folders.

     

    The version of  template for NetTiers says

    <%--
     * $Id: NetTiers.cst,v 1.19 2006/03/07 09:51:43 jroland Exp $
     * Last modified by $Author: goofsr $
     * Last modified at $Date: 2006-05-24 03:22:50 +0200 (mer., 24 mai 2006) $
     * $Revision: 188 $

     Description:
      NetTiers main templates. Internally call sub templates to genereate entities and dal. 
     
     Originally written By Ryan Hurdon
     ReWritten and maintained by John Roland since Oct. 2004
    --%>

     

    thanks

    Nick

     

     

  • vd.JPG

    I did move my project from C:\Documents and Settings\pateln.PROB\My Documents\Visual Studio 2005\WebSites\WH\WareHouse to C:\C_WH.  The application name is correct as you showed me in your picture.  The local path is correct also.  By the way how did you get the picture into the posting. Maybe I can show you how my IIS and Explorer looks like.

    thanks

    Nick


  • Nick,

    When you're replying under option tab there is 'Add/Update' Attachment button (images automatically displayed in the message)

    Looks like you're using .net framework 2.0, what framework version you have set in IIS for this project (it could be found under ASP.NET tab)

    Michael

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

  • ver.JPG
    yes I am using version 2.0.  I have attached the pictures showing IIS and Explorer.

  • explorer.JPG

    Here is how the explorer looks like showing I have the right NetTier names


  • In your original post, I noticed this in part of the error:

    Source File: c:\C_WH\WareHouse2\App_Code\DAL\NetTier\NetTier.DAL\DataRepository.cs    Line: 76

    In case you missed it, the class that is throwing the error is not the same as the one you have defined in your web.config:

    <section name="netTiersService" type="NetTier.DAL.Bases.NetTiersServiceSection, NetTier.DAL"/>

    The source file class is in the App_Code assembly while the config file points to the NetTier.DAL assembly.  These must match in order for this line to evaluate to true:

    if ( temp is NetTiersServiceSection )

    Even though they have the same namespace and class name, they are not considered the same because the assembly information is part of the type description.

    Hope that helps.

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
  • explorer.JPG

    In the explorer, the DataRepository.cs resides in C:\C_WH\WareHouse2\App_Code\DAL\NetTier\NetTier.DAL directory.  The NetTiersServiceSection.cs seems to reside in C:\C_WH\WareHouse2\App_Code\DAL\NetTier\NetTier.DAL\Bases.  When I ran the template, this is where it put it.  Is there something I should change in the <section name> to correct this?  Here is the view of VS2005 explorer:

     


Page 1 of 2 (19 items) 12