?
Solved

DLL loads Machine.config instead of my App.config

Posted on 2009-05-15
3
Medium Priority
?
1,057 Views
Last Modified: 2012-05-07
Hi All,

I'm having a strange issue I've never ran into before.  I have a VS2008 solution that contains a web service project, a dll project that contains core functions/types and a dll project that has all of my data access code in it.  My web service has a web.config file that works fine, can load properties no problem.  My data access project didn't have an app.config file so I added one using, Add->New Item->Application Configuration File which created an app.config file.  I put my connection string code in, like so:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=MyMachine\\SQLEXPRESS;Initial Catalog=MyDB;Integrated security=SSPI;" />
  </connectionStrings>
</configuration>

In my data access dll, I try and access the connection string like so:

string cnstring = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString.ToString();

This fails with a null reference error.  So, to try and find out what was going on, I used the following code to try and see what it was using:

ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
string test = ConfigurationManager.AppSettings.ToString();

if (settings != null)
{
     foreach (ConnectionStringSettings cs in settings)
     {
          Console.WriteLine(cs.Name);
          Console.WriteLine(cs.ProviderName);
          Console.WriteLine(cs.ConnectionString);
     }
}

The value for the name in the connection string is "LocalSQLServer" which tells me it isn't loading my App.config file for some reason.  I did some searching and found that it's possible to remove items from the App.Config file so I changed my App.config file to look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <clear/>
    <remove name="LocalSQLServer" />
    <add name="MyConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=MyMachine\\SQLEXPRESS;Initial Catalog=MyDB;Integrated security=SSPI;" />
  </connectionStrings>
</configuration>

I still get the same results, it only loads one connection string and it's name is "LocalSQLServer".

Has anyone seen this before or could lend some insight to why this is happening?

Thanks in advance,

Craig
0
Comment
Question by:ichikuma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
b_levitt earned 2000 total points
ID: 24394442
Everything uses machine.config.  Other config files are just applied on top of it.

I think I understand what's going on.  Your expecting your dll to use a separate app.config.  But if you're using this dll in a web project, then the config file loaded for the process IS the web.config.   Which means you need to add your code from your app.config to your web.config.
0
 

Author Closing Comment

by:ichikuma
ID: 31581876
Thanks so much for the insight and for the explanation.  It makes total sense now and it works perfectly by adding it to the web.config.

Thanks so much for your help,

Craig
0
 
LVL 11

Expert Comment

by:b_levitt
ID: 24400713
no problem.  I'm glad one post is all you needed.  Good luck.

B
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question