Configuration Problem

Topics: Data Access Application Block
Feb 13, 2007 at 5:58 PM
I’m having some problems integrating some prototype concepts into some demo code and am wondering if anybody has any ideas.

In my prototype, I created a Windows Forms Application project. In the project, I created a new App.Config. I opened the App.Config file with the EntLibConfig.exe and deleted the existing DAAB stuff it picks up from the Machine.config. I created a new configuration source (it gave me System Configuration) and then I created a new File Configuration Source. I set the file to be c:\external.config (a la Tom Hollander’s blog) and set the selected source to be the File Configuration Source. To finish it off, I opened external.config (without closing App.config) and added a new DAAB. I created a new Connection string, gave it a name and supplied the appropriate database information. I saved everything and closed the Ent Lib console. Do you agree with the way I've set up configuration?

Back in the Windows Forms project, I added references to EnterpriseLibrary.Common, EnterpriseLibrary.Data and ObjectBuilder. In the code behind Form1.cs, I added using Microsoft.Practices.EnterpriseLibrary.Data and using System.Data.Common. In a button click event, I added the following code:

Database myDb = DatabaseFactory.CreateDatabase("SQL DEV Instance");
DbCommand myCmd = myDb.GetSqlStringCommand("SELECT * FROM UNF.UF_TAG");
DataSet ds = myDb.ExecuteDataSet(myCmd);
dataGridView1.DataSource = ds.Tables0;

It’s dead simple. The prototype concept works. However, when I roll this into our demo code I consistently get an error with Database myDb = DatabaseFactory.CreateDatabase("SQL DEV Instance") and the error is The requested database SQL DEV Instance is not defined in configuration. If I look at my development environment, it is different from the prototype in that my project is a Class Library that is loaded by another executable. I set up my configuration identically to what I’ve listed above, but it doesn’t seem to be able to find my configuration. Is the configuration source folder specific? Our file structure is :
C:\Program Files\MyCompany\OurFramework\MyClassLibrary

In the “OurFramework” folder, we have an executable (say ABC.exe). In “MyClassLibrary” folder is my assembly (say Demo.dll). When we run ABC.exe, it loads Demo.dll and I can create instances of user controls and add them to a panel or form. That part works but I cannot seem to read the configuration information in Demo.dll.config (my App.config). Any ideas why?

Sorry for the long winded note but I wanted to try and convey as much information as possible to help explain the situation.
Feb 14, 2007 at 12:57 AM
Hi Roger -

This is how .NET configuration works - DLL projects can't "own" configuration files - they must be owned by the host process (ASP.NET, Windows Forms, console, etc). So the simplest solution is to add the configuration to your ABC.exe's app.config file (which will get copied to ABC.exe.config).

If this isn't what you want, Enterprise Library does provide some options to let a DLL own its own config. Check out this post for a list of the options.