0

Here is my App.Config

<?xml version="1.0"?> 
<configuration> 
  <connectionStrings> 
    <clear/>
    <add name="con1" connectionString="..." /> 
    <add name="con2" connectionString="..."/> 
    <add name="con3" connectionString="..."/> 
    <add name="con4" connectionString="..." /> 
    <add name="con5" connectionString="..."/> 
  </connectionStrings> 
  <appSettings> 
    <add key="S...
  ...
</configuration>

I have a breakpoint that I'm hitting right before I try to create a database connection, and in the Immediate Window, I run this:

?ConfigurationManager.ConnectionStrings.Item(0).ConnectionString 

but it returns this:

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

Which isn't defined anywhere. (All my connections are remote MSSQLServer instances)

doing ?ConfigurationManager.ConnectionStrings(1) gives an Index out of range error, so there is only one entry in my connection strings settings object :-(

What's going on? this feels crazy.

I'm using VS2013, VB.NET, and .NET 4.0

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • Is this an asp.net project? If so, then I don't think it will use app.config. Does your project have a web.config file? – CoderDennis Mar 13 '15 at 19:27
  • it's a windows application project. There is only the app.config – NullVoxPopuli Mar 13 '15 at 19:28
  • It's not using your app.config file, and therefore picking up a connection string that's probably defined in machine.config. When you build a Windows app, the build process will convert app.config into a file "MyApp.exe.config" in the same directory as your executable "MyApp.exe". Check that this file exists, and look at its contents. – Joe Mar 13 '15 at 19:32
  • something to check...once an app gets compiled and sent to is folder (under bin\debug or bin\release), there should be a file names .config. open that with notepad, and ensure that it contains the right settings. – Jeremy Mar 13 '15 at 19:32
  • I looked in bin/ but didn't see debug or release, but I did see the program.exe, but no config. I found debug and release in obj/, but neither debug nor release contain a program.exe.config :-( – NullVoxPopuli Mar 13 '15 at 19:35
  • check out http://stackoverflow.com/questions/23296098/appconfig-file-not-found-in-bin-directory – Jeremy Mar 13 '15 at 19:39
  • That helps. I also found out that when I went to "Add an existing file" to my unit test project, it copied the file to the unit test directory... so I DID have two App.Configs – NullVoxPopuli Mar 13 '15 at 19:45
  • now, actually my tests don't pass, because it things that my configuration is empty. just as it did before. Maybe that's why I originally added App.config to my test project. Is there a way around this? – NullVoxPopuli Mar 13 '15 at 19:58
  • fixed it! had to make sure App.Config was copied to the output directory :-) – NullVoxPopuli Mar 13 '15 at 20:00

0 Answers0