3

I am using an abstract class to get the ConnectionString from my App.config file. However instead of the ConnectionString specified the app comes up with ...

{data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}

I have no idea where this connection string came from. I don't have SQL Express installed. I know the proper App.config is not be accessed because I added a dummy connection string named foo and only one connection string is picked up when I step through debugging instead of two. It's like the app is getting the connection string from another App.config!

Here is my class

using System;
using System.Configuration;

namespace MyApiUpdater.Data
{
    public abstract class ConnectionAccess
    {

        protected string ConnectionString
        {
            get 
            {
                return ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
            }
        }
    }
}

and here is my App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="MyDatabase" connectionString="Server=MyDatabaseServer;Database=MyDBName;Trusted_Connection=True;" />\
        <add name="foo" connectionString="Server=MyDatabaseServer;Database=MyDBName;Trusted_Connection=True;" />
    </connectionStrings>
</configuration>
webworm
  • 10,587
  • 33
  • 120
  • 217
  • Do you have multiple projects in your solution? If for example you are running from a unit test project, it will use the app.config from the unit test project. – crthompson Jul 24 '14 at 23:53
  • Re "I have no idea where this connections string came from," it might help to just CTRL+F `=.\SQLEXPRESS` in the entire solution. The above commenter has a great point, which is probably causing your issue. But this might be an easier way of pinpointing where it's coming from. – Matthew Haugen Jul 24 '14 at 23:56
  • I do indeed have two projects and the other project does have an App.config. However, not connection string info is contained within that App.config. This second project is the UI that uses classes in the first project. – webworm Jul 25 '14 at 00:00
  • and I did do a search for `=.\SQLEXPRESS` in the entire solution and it could not be found. – webworm Jul 25 '14 at 00:07
  • I suspect that your application is not using the config file you think it is using – Rob Tillie Feb 27 '15 at 09:03

1 Answers1

0

Check out this: Override machine.config by web.config

Also, make absolutely sure that you are in the correct config file in Visual Studio as you can unexpectedly grab and edit one from the wrong folder and in doing so your app grabs the connection string in the machine.config.

See: Where Is Machine.Config? and look at the file and you will see where the wrong string is coming from.

Additional tag: web.config

Community
  • 1
  • 1
Reid
  • 3,170
  • 2
  • 23
  • 37