views:

10602

answers:

3

Hi all,

I am stuck! this seems really daft but I can not see where I am going wrong. I am creating a 2.0 C# ASP.NET website. I am trying to use a custom section in the web.config file with:

DatabaseFactorySectionHandler sectionHandler = ConfigurationManager.GetSection("DatabaseFactoryConfiguration") as DatabaseFactorySectionHandler;

I have a separate DLL for the Objects which are in Bailey.DataLayer namespace. But when I run the test.aspx page I get the following error:

System.Configuration.ConfigurationErrorsException was unhandled by user code

Message="An error occurred creating the configuration section handler for DatabaseFactoryConfiguration: Could not load file or assembly 'Bailey.DataLayer' or one of its dependencies. The system cannot find the file specified. (C:\\Documents and Settings\\Administrator.PIP\\My Documents\\Visual Studio 2005\\WebSites\\bailey\\web.config line 13)"
Source="System.Configuration"

The class that I am trying to get is as follows:

namespace Bailey.DataLayer

{

public sealed class DatabaseFactorySectionHandler : ConfigurationSection

{

    [ConfigurationProperty("Name")]

    public string Name

    {

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

    }



    [ConfigurationProperty("ConnectionStringName")]

    public string ConnectionStringName

    {

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

    }



    public string ConnectionString

    {

        get

        {

            try

            {

                return ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;

            }

            catch (Exception excep)

            {

                throw new Exception("Connection string " + ConnectionStringName + " was not found in web.config. " + excep.Message);

            }

        }

    }

}

}

The web config file has this section:

<section name="DatabaseFactoryConfiguration" type="Bailey.DataLayer.DatabaseFactorySectionHandler, Bailey.DataLayer" />

I have done this in a console app without a problem but can not see any differences apart from it being in a web page.

EDIT

It all compiles and throws the error at runtime so I can only assume it find the assembly because it is referenced in the test.aspx.cs page.

I have the following using statement at the top of the test.aspx.cs page:

using Bailey.DataLayer;

Here is the whole web.config file so there is no confusion:

<configuration>
   <configSections>
      <section name="DatabaseFactoryConfiguration" type="Bailey.DataLayer.DatabaseFactorySectionHandler, Bailey.DataLayer" />
   </configSections>
    <appSettings/>
   <connectionStrings>
      <add name="BaileyMDFConString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bailey.mdf;Integrated Security=True;User Instance=True"  providerName="System.Data.SqlClient" />
    </connectionStrings>
     <DatabaseFactoryConfiguration Name="System.Data.SqlClient" ConnectionStringName="BaileyMDFConString" />
   <system.web>      
      <compilation debug="true"/>    
      <authentication mode="Windows"/>  
   </system.web>
  </configuration>

I hope someone can put me out of my missery.

Thanks

A: 

You need two entries in the config file, one on the configSections element to declare the custom config section, and another - the actual custom config section itself. Did you add both?

for example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    **<section name="Connections"
             type="System.Configuration.DictionarySectionHandler" />**
  </configSections>

  <Connections 
        <add key="myServer" value="serverName" />
        <add key="myPort"   value="8080" />
        <add key="myURI"    value="RequestUri" />
        <add key="UserId"   value="joebob" />
        <add key="password" value="$^%^&%$^&@%" />        
   />

</configuration>
Charles Bretana
Hi Charles, I have edited my post to try and show the whole config file (not displaying correctly atm. But I believe it is correct. I didn't know about the DictionarySectionHandler which is cool and something I will use in the future. thx
Jon
+2  A: 

Either you're using the wrong name (i.e. it's not called Bailey.DataLayer.dll), or it's not being copied to the bin directory on build. This last one doesn't seem likely however.

(See my comments on the question for clarification).

Andrew Rollings
Thank you Andrew! It clicked. the DLL name was not being referenced in the web config. I didn't realiize that it was the actual DLL I had to put, I thought it was just the name space.
Jon
A: 

You may want to have a look at http://www.primaryobjects.com/CMS/Article81.aspx which contains, almost line for line, the code that you have shown above, including an example from a web.config file.