Reputation: 12526
Background
I have a Windows Forms application (.NET 3.5). The database connection string is stored in the app.config
file in the appSettings
section with the name "connectionString".
<appSettings>
<add key="connectionString" value="Data Source=REDACTED;Initial Catalog=REDACTED; UID=REDACTED; pwd=REDACTED;" />
</appSettings>
This is accessed in the code for database queries with the following code:
dbConnString = ConfigurationSettings.AppSettings["connectionString"];
Setup
Because I am having problems, I littered the startup and applicable sections of the application with MessageBox.Show()
statements, each giving relevant information about where I am in the program and what the variable values are.
I have an installer project that creates and MSI file. I take that file, copy it to the client's machine, and install it (if the application is already installed, I uninstall it first).
The Problem
I am seeing some REALLY strange behaviour...
Data Source=REDACTED;Initial Catalog=REDACTED; UID=REDACTED; pwd=REDACTED;
)and connects the application to the database without issue. Notice that the connection string is using SQL Server authentication (not Windows authentication).Data Source=REDACTED;Integrated Security=SSPI;Initial Catalog=REDACTED
. Notice that this is using Windows authentication to try to connect to the database.app.config
file completely, the application still runs (huh??? how??) but with some differences:connectionString
and I get the error `Object reference not set to an instance of an object.'connectionString
) in the app.config
file. So I named it connectionStringDude
. Now when I run it in the two ways described above, again the "Run As Administrator" version works, but the other one fails to find a connection string by that name, and the application gets an `Object reference not set to an instance of an object.'.So it appears to me that there is maybe some hierarchy that the application is using to look for the connection string connectionString
. If the framework can't find the connection string in the app.config, it checks machine.config, and so on until it either finds it or doesn't.
I am tearing my hair out trying to figure this out. Why would the connection string CHANGE based just on "Run as administrator"?
And just to be clear, the SAME line of code:
dbConnString = ConfigurationSettings.AppSettings["connectionString"];
is returning a COMPLETELY different value in dbConnString depending on if I Ran as Administrator or not.
Upvotes: 0
Views: 152
Reputation: 256911
Use Process Monitor to see where Windows is loading your application settings from.
Configuration Settings in .NET application are persisted in separate app.exe.config
files. You likely have a stray file somewhere.
And with UAC file virtualization, that file could be sitting in something like:
C:\Users\Richard\AppData\Local\VirtualStore\Program Files(x86)\RichardWare\SuperCoolapp.exe.config
Use Process Monitor to watch all file accesses of your app, especially *.config
files.
Upvotes: 1