Activation error occured while trying to get instance of type Database, key "cnx"

Topics: Data Access Application Block
Jan 29, 2013 at 7:22 AM

I am trying to use EntLib 5.0 with VS 2010

i am getting following error:

Activation error occured while trying to get instance of type Database, key "cnx"

Inner exception

 

{"Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.Data.Database\", name = \"cnx\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type Database does not have an accessible constructor.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n  Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,cnx\r\n"}

more details :

[InvalidOperationException: The type Database does not have an accessible constructor.]
   Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObject(IBuilderContext context) +228
   BuildUp_Microsoft.Practices.EnterpriseLibrary.Data.Database(IBuilderContext ) +110
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +319
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +260
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +373

[ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "cnx".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database does not have an accessible constructor.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,cnx
]
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +436
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides) +50
   Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) +48
   Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceType, String key) +156
   Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:49

[ActivationException: Activation error occured while trying to get instance of type Database, key "cnx"]
   IMSDataLayer.daoUsers.SelectOne(Int32 ID) in C:\inetpub\wwwroot\websites\APCInnovationManagementSystem\IMSDataLayer\daoUsers.cs:89
   IMSDataLayer.daoUsers.GetUserDetailsByID(Int32 _uID) in C:\inetpub\wwwroot\websites\APCInnovationManagementSystem\IMSDataLayer\daoUsers.cs:74
   IMSBusinessLayer.baoUsers.getUserDetails(Int32 _usrID) in C:\inetpub\wwwroot\websites\APCInnovationManagementSystem\IMSBusinessLayer\baoUsers.cs:18
   APCInnovationManagementSystem.Maintain.Login.btnCMSLogin_Click(Object sender, EventArgs e) in C:\inetpub\wwwroot\websites\APCInnovationManagementSystem\APCInnovationManagementSystem\Maintain\Login.aspx.cs:22
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553178
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

Please advise .. thanks

srini

Jan 29, 2013 at 1:26 PM

Have you configured a database connectionString with the name "cnx"?

The app.config or web.config should look something like:

<?xml version="1.0"?>
<configuration>
<configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="cnx" />
<connectionStrings>
    <add name="cnx" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Database=DatabaseName;"
        providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Because the default database is configured then I can retrieve the Database using no arguments:

Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();

Or I can pass in the connection string name:

Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("cnx");

Also the assembly cannot target .NET Framework Client Profile.

If you are still having issues, it would help to post the configuration and a code snippet.

--
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to 

Jan 30, 2013 at 6:36 AM
thank you Randy,
it worked for me.
Regards, Srini.
randylevy wrote:

Have you configured a database connectionString with the name "cnx"?

The app.config or web.config should look something like:

<?xml version="1.0"?>
<configuration>
<configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="cnx" />
<connectionStrings>
    <add name="cnx" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Database=DatabaseName;"
        providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Because the default database is configured then I can retrieve the Database using no arguments:

Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();

Or I can pass in the connection string name:

 

Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("cnx");

 

Also the assembly cannot target .NET Framework Client Profile.

If you are still having issues, it would help to post the configuration and a code snippet.

--
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to