suchetawieser
asked on
using .config file for Windows Application to read connection string for SQL Server gives error message
new to C# and I don't know what's wrong
I want to put the values for my database connection strings in a configuration file.
This is how my .config file looks
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="sqlConnVisitor.Connec tionString " value="Data Source=mullah\\Developer;
Integrated Security=SSPI; Initial Catalog=VisitorCenter" />
<add key="sqlConnWelcome.Connec tionString " value="Data Source=mullah\\Developer;
Integrated Security=SSPI; Initial Catalog=Contacts" />
</appSettings>
</configuration>
The code where I'm connecting to the database looks like this:
public static void ConnectToDatabase()
{
System.Configuration.AppSe ttingsRead er configurationAppSettings = new
System.Configuration.AppSe ttingsRead er();
sqlConnVisitor = new SqlConnection();
sqlConnVisitor.ConnectionS tring = ((string)(configurationApp Settings.G etValue
("sqlConnVisitor.Connectio nString", typeof(string))));
sqlConnWelcome = new SqlConnection();
sqlConnWelcome.ConnectionS tring = ((string)(configurationApp Settings.G etValue
("sqlConnWelcome.Connectio nString", typeof(string))));
sqlConnVisitor.Open();
sqlConnWelcome.Open();
}
When I run the application I get the following error message:
An unhandled exception of type 'System.InvalidOperationEx ception' occurred in system.dll
Additional information: The key 'sqlConnVisitor.Connection String' does not exist in the appSettings
configuration section.
Anyone know what to do? Thanks
I want to put the values for my database connection strings in a configuration file.
This is how my .config file looks
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="sqlConnVisitor.Connec
Integrated Security=SSPI; Initial Catalog=VisitorCenter" />
<add key="sqlConnWelcome.Connec
Integrated Security=SSPI; Initial Catalog=Contacts" />
</appSettings>
</configuration>
The code where I'm connecting to the database looks like this:
public static void ConnectToDatabase()
{
System.Configuration.AppSe
System.Configuration.AppSe
sqlConnVisitor = new SqlConnection();
sqlConnVisitor.ConnectionS
("sqlConnVisitor.Connectio
sqlConnWelcome = new SqlConnection();
sqlConnWelcome.ConnectionS
("sqlConnWelcome.Connectio
sqlConnVisitor.Open();
sqlConnWelcome.Open();
}
When I run the application I get the following error message:
An unhandled exception of type 'System.InvalidOperationEx
Additional information: The key 'sqlConnVisitor.Connection
configuration section.
Anyone know what to do? Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and one step further, gets you:
public static void ConnectToDatabase()
{
sqlConnVisitor = new SqlConnection(getConnectio nString("s qlConnVisi tor"));
sqlConnWelcome = new SqlConnection(getConnectio nString("s qlConnWelc ome"));
sqlConnVisitor.Open();
sqlConnWelcome.Open();
}
which is even clearner.
AW
public static void ConnectToDatabase()
{
sqlConnVisitor = new SqlConnection(getConnectio
sqlConnWelcome = new SqlConnection(getConnectio
sqlConnVisitor.Open();
sqlConnWelcome.Open();
}
which is even clearner.
AW
hehe I think my way is shorter and cleaner... :)
check to make sure the .config file has the format of executablename.exe.config in the output folder. If not then re-add a new item to the project selection the App Config file with the default filename.
Can we close this question?
I concur. It would appear that suchetawieser has no interest in participating in this discussion. I suggest that the points NOT BE REFUNDED.
AW
AW
I agree...split the points :)
private string getConnectionString(string
{
return ((string)(configurationApp
(WhichOne,typeof(string)))
}
public static void ConnectToDatabase()
{
System.Configuration.AppSe
System.Configuration.AppSe
sqlConnVisitor = new SqlConnection();
sqlConnVisitor.ConnectionS
sqlConnWelcome = new SqlConnection();
sqlConnWelcome.ConnectionS
sqlConnVisitor.Open();
sqlConnWelcome.Open();
}
which is somewhat easier to read and understand.
AW