G
Guest
I've created a windows forms appliation that gets its data from a sql database.
I want to create an app.config file for this application so that in the
event that the server name changes, they won't have to recompile to connect,
but rather change the setting in the app.config file.
I've seen many examples of this online and have done this a number of times
myself in ASP.net with the web.config file, however, I am having issues
getting it to work this time.
Here is the App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Password=Password;Persist Security
Info=True;User ID=User;Initial Catalog=Database;Data Source=DatabaseServer;"
/>
</appSettings>
</configuration>
And in my DAL i access this key the following way...
private static string sqlConn =
ConfigurationSettings.AppSettings["ConnectionString"];
I have included the reference to System.Configuration in the DAL.
This code compiles, but gives me a runtime error that states "the connection
string property has not been initialized".
When I MessageBox.Show(sqlConn)... I see nothing.
When I statically define sqlConn, everything works fine.
Do I need to somehow explicitly tell the windows forms project to look at
the App.config file?
Also, I have renamed the app.config file to ProgramName.Config. Does this
have any ramifications? (It doesn't work when I change it back to
App.config).
Thanks in advance for your help!
-Paul
I want to create an app.config file for this application so that in the
event that the server name changes, they won't have to recompile to connect,
but rather change the setting in the app.config file.
I've seen many examples of this online and have done this a number of times
myself in ASP.net with the web.config file, however, I am having issues
getting it to work this time.
Here is the App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Password=Password;Persist Security
Info=True;User ID=User;Initial Catalog=Database;Data Source=DatabaseServer;"
/>
</appSettings>
</configuration>
And in my DAL i access this key the following way...
private static string sqlConn =
ConfigurationSettings.AppSettings["ConnectionString"];
I have included the reference to System.Configuration in the DAL.
This code compiles, but gives me a runtime error that states "the connection
string property has not been initialized".
When I MessageBox.Show(sqlConn)... I see nothing.
When I statically define sqlConn, everything works fine.
Do I need to somehow explicitly tell the windows forms project to look at
the App.config file?
Also, I have renamed the app.config file to ProgramName.Config. Does this
have any ramifications? (It doesn't work when I change it back to
App.config).
Thanks in advance for your help!
-Paul