Now I get this error:
An unhandled exception of type
'System.Configuration.ConfigurationException'
occurred in system.dll
Additional information: Unrecognized configuration section appSetting
Here is my code, password as be removed for security.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetting>
<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />
</appSetting>
</configuration>
I have tried both, string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; and string
sConnString = ConfigurationSettings.AppSettings["dsn"];
I get the error on both.
Added info, if this helps. The call to string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"]; is on Load
of
my Win Form named frmDataEntry, and the file name for storing the value is
named App.config
Mark White said:
Mike
It would be:
System.Configuration.ConfigurationSettings.AppSettings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];
I get the error: The type or namespace name 'ConfigurationSettings'
could
not be found (are you missing a using directive or an assembly
reference?)
I also tried: string sConnString = configuration.appSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not
be
found (are you missing a using directive or an assembly reference?)
Here is the App.config code, I removed the password for security.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetting>
<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />
</appSetting>
</configuration>
:
I would go with a App.config file. That is exactly it's purpose,
it's
secure and already accessible. No need to reinvent the wheel.
Add a key (XML format) to the App.config file, then use
System.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];
Regards,
Mark
This is for a Win form.
Currently I have several connection strings that are identical
through
out
my application. Is a global variable the best choice for this
situation?
If
so, what is the code to make a global string variable, and where do
I
put
it,
(before the namespace, of just under it)?????