app.config


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
 
Ad

Advertisements

G

Guest

OK... this is weird.
I noticed that the app.config file wasn't being copied into the Debug
directory after i compiled the application, so I did it manually. It didn't
work... so I changed the name to App.exe.config and it worked perfectly.

Now I need to find out how to tell Visual Studio to include this file in teh
Debug directory when compiling... I'll post once I find out incase others run
into this issue.

-Thanks,

Paul
 
J

John Vottero

Paul Daly (MCP) said:
OK... this is weird.
I noticed that the app.config file wasn't being copied into the Debug
directory after i compiled the application, so I did it manually. It
didn't
work... so I changed the name to App.exe.config and it worked perfectly.

Now I need to find out how to tell Visual Studio to include this file in
teh
Debug directory when compiling... I'll post once I find out incase others
run
into this issue.

Some of what you're saying doesn't make sense. I don't think that
App.exe.config will work unless your executable is App.exe.

You're supposed to create a file named App.config in your Visual Studio
project and when you build, Visual Studio will copy App.config to
Programname.exe.config in the output directory. You said that you tried
Programname.exe.config but, was that the name in the output directory or in
Visual Studio? Visual Studio won't copy that to the output directory, it's
only looking for App.config.

-Thanks,

Paul

Paul Daly (MCP) said:
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
 
M

Marc Scheuner [MVP ADSI]

OK... this is weird.
I noticed that the app.config file wasn't being copied into the Debug
directory after i compiled the application, so I did it manually. It didn't
work... so I changed the name to App.exe.config and it worked perfectly.

This isn't weird - this is the way is it supposed to be ! The file
needs to be in your EXE's directory, and must be called
"YourApp.EXE.config". That's the only way it'll work.
Now I need to find out how to tell Visual Studio to include this file in teh
Debug directory when compiling... I'll post once I find out incase others run
into this issue.

You must make sure the file is a straight text file, and it HAS TO BE
CALLED "app.config" in your VS.NET project - exactly that, DO NOT
REPLACE "app" with your app's name or anything! In that case, VS.NET
will automatically rename it to "yourprogram.exe.config" and place it
in the output directory of a compilation.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Ad

Advertisements

G

Guest

You are both right...

My problem was that I changed the name of the file within VS (before compile
time) to AppName.exe.config, rather than leaving it App.config. However,
once I changed this back to App.config, it still didn't work for some reason.
The AppName.exe.config file wasn't being automatically generated by VS.
After deleteing and recreating the file (making sure not to alter the name
this time) everything worked as it should.

Thanks for both of your help...

-Paul
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top