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

Topics: Data Access Application Block
Nov 25, 2011 at 3:27 AM
Edited Nov 25, 2011 at 3:30 AM

I am getting this error

The type Database cannot be constructed. You must configure the container to supply this value

Database db = DatabaseFactory.CreateDatabase();

app.config

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <dataConfiguration defaultDatabase="Connection String" />
    <connectionStrings>
        <add name="Connection String" connectionString="Database=Bancos;Server=(local)\MSSQLSERVER;Integrated Security=SSPI"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

 

  

I am using:

Enterprise Library 5.0

C# 2008

SQL Server Developer Edition 2008 R2

Winform application

Nov 25, 2011 at 5:42 AM

The posted configuration is fine.  The error message is basically pointing to the configuration file not being loaded.
Are you sure that the configuration (e.g. WindowsFormsApplication1.exe.config) is being deployed along with the executable?

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

Nov 25, 2011 at 2:07 PM
Edited Nov 25, 2011 at 2:14 PM

Hi,

No exe.config file is been generated, but I think that the configuration file is being deploying in the Datos.dll.config file, here I have the connection string.  

I am using SQL Server 2008 Developer R2 this can generate a problem ?. Few day ago I was using SQLExpress and I did not had this problem.

Nov 25, 2011 at 2:32 PM

The version of SQL does not matter since when you call CreateDatabase it does not try to connect to a database -- it just reads the configuration in order to construct the appropriate object(s).

If your configuration is in a *.dll.config file then the configuration will not be automatically loaded by .NET.  You should move the configuration into app.exe.config.

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

Nov 25, 2011 at 4:56 PM
Edited Nov 25, 2011 at 4:57 PM

I changed the app.config

<add name="Connection String" connectionString="Database=Bancos;Server=(local)\ ;Integrated Security=SSPI" providerName="System.Data.SqlClient" />

and the actual error message is "Object reference not set to an instance of an object." with an inner exception of "Method may only be called on a Type for which Type.IsGenericParameter is true."

What this error means ?

 


Nov 25, 2011 at 5:28 PM

It looks like you've changed the connection string a bit (no instance name).  

To recap: in your app.config you have the full configuration above:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <dataConfiguration defaultDatabase="Connection String" />
    <connectionStrings>
        <add name="Connection String" connectionString="Database=Bancos;Server=(local)\MSSQLSERVER;Integrated Security=SSPI"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

And now when you call 

Database db = DatabaseFactory.CreateDatabase();

a NullReferenceException is being thrown.

Or is the exception being thrown in a different place now?

 

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

Nov 25, 2011 at 6:10 PM

I m sorry, I didn't explain well, this is the app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <dataConfiguration defaultDatabase="Connection String" />
    <connectionStrings>
        <add name="Connection String" connectionString="Data Source=.\;Initial Catalog=Bancos;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>
when I call: Database db = DatabaseFactory.CreateDatabase();
the actual error message is: "Activation error occured while trying to get instance of type Database, key \"\"" with an inner exception of "The type Database cannot be constructed. You must configure the container to supply this value."
this error comes when I try to create a Bank.
On the other hand, when I call: string connectionString = ConfigurationManager.ConnectionStrings["Connection String"].ConnectionString;
the actual error message is: "Object reference not set to an instance of an object." with an inner exception of "Method may only be called on a Type for which Type.IsGenericParameter is true."
this error comes when I try to list a Bank.
So the are two different errors. I hope I have explained better.
Nov 25, 2011 at 8:08 PM

I'm still a bit confused...When you call ConfigurationManager.ConnectionStrings["Connection String"].ConnectionString;  it throws an exception?
Or when you actually try to connect to the database? 

Instead of going back and forth, It would probably be better if you email me a (small) project that reproduces the issue.

Thanks,

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

Nov 25, 2011 at 9:02 PM
Hi this is little project to test. Thanks and regards !


Nov 25, 2011 at 9:08 PM
Edited Nov 25, 2011 at 10:05 PM

I send to you an email with the code.

It could be a dll problem ? for example with Microsoft.Practices.ServiceLocation.dll

Another thing that I did, was to test the Enterprise Library 5.0 - Hands On Labs in especial the DAAB. I tried with the first exercise, the one that is completed and it worked fine.

Then I tried with the first exercise, the one that is not completed and it did not worked. The difference that I saw was that CustomerManagement.exe.config file was generated,

and when I build the solution that I completed, the CustomerManagement.exe.config file was not generated, So I copied and pasted the file from one solution to another and it worked.

Why when I build the solution the CustomerManagement.exe.config  file is not generated ?

This fixed the first error : "Activation error occured while trying to get instance of type Database, key \"\""

Nov 25, 2011 at 10:11 PM

I didn't receive your email.  Can you try again to: entlib.support@live.com.

Thanks, 

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

Nov 25, 2011 at 10:25 PM
Edited Nov 25, 2011 at 10:26 PM

OK, I just sent the email, thanks !

Nov 26, 2011 at 12:53 AM

Hi,

In your test project I copied the App.config from the Datos Class Library project to the Presentacion Windows Form project.

Once I did that I was able to run your code up to the point where the connection is opened.  This fails since I don't have your database setup here.

Hopefully that should resolve your issue.

 

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