locked
Using Enterprise Library from Data Access Layer RRS feed

  • Question

  • User1437711547 posted

    I am working on an ASP.Net website where I have used the layered architecture approach.

    I have a separate layer for Data Access, Business logic, Presentation etc.

    I get the following error when trying to use Enterprise Library Data Access app block.

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

    After researching on net, I found out that to fix the problem the connection string needs to be supplied to the CreateDatabase class construcor as shown in the statement below.

    Database db = DatabaseFactory.CreateDatabase("<Connection string from config file>");

    The afore-mentioned code would typically be part of Data access layer (which is a separate class library project).

    How the Data access layer should read the configuration information being a class library project? Usually, the web.config file exists in the Web application project.

    Should we have an app.config file in the Data access library project or which other way configuration information (connection string) can be read from a config file from a data access library project?

    Thanks!

    Tuesday, November 29, 2011 9:13 PM

Answers

  • User1643008609 posted

    Hi Friend,

    The class library project's dll can access the web.config in the web application it is used.

    Use ConfigurationManager.ConnectionStrings.

    http://forums.asp.net/t/1394239.aspx/1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 29, 2011 11:25 PM
  • User1805528910 posted

    Do the following setting into ConfigSection

    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null"/>

    <dataConfiguration defaultDatabase="ConnectionString"/>

    <connectionStrings>
            <add name="ConnectionString" connectionString="Yuur Connection String" providerName="System.Data.SqlClient"/>
        </connectionStrings>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 30, 2011 12:20 AM

All replies

  • User1643008609 posted

    Hi Friend,

    The class library project's dll can access the web.config in the web application it is used.

    Use ConfigurationManager.ConnectionStrings.

    http://forums.asp.net/t/1394239.aspx/1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 29, 2011 11:25 PM
  • User1805528910 posted

    Do the following setting into ConfigSection

    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null"/>

    <dataConfiguration defaultDatabase="ConnectionString"/>

    <connectionStrings>
            <add name="ConnectionString" connectionString="Yuur Connection String" providerName="System.Data.SqlClient"/>
        </connectionStrings>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 30, 2011 12:20 AM
  • User1437711547 posted

    To close this thread, I would like to mention here what solved the issue for me.

    As Hemant copied the configuration above, the following statement was missing from my web.config where the default database needs to be specified. The DatabaseFactory.CreateDatabase() method takes the following configuration into consideration in its constructor if no config varible or connection string is specified.

    <dataConfiguration defaultDatabase="ConnectionString"/>

    Please point out if my understanding is incorrect. I had resolved this issue sometime back. From what I remember is this and I thought of sharing the same here in the forum.

    --raringsunny

    Wednesday, December 21, 2011 5:56 PM