Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with reading web.config 1

Status
Not open for further replies.

Henryas

Programmer
Mar 28, 2005
16
0
0
US
Hi,

I have a problem with reading web.config. I can't seem to figure out what's wrong with it.

Below is a snippet of my codes

Code:
[green]//web.config[/green]
<configuration>
  <appSettings>
		<add key="connectionString" value="server=(local);user=username;password=blabla;database=mydb;Trusted_Connection=yes" />
  </appSettings>
</configuration>

[green]//in the source file[/green]

string c = ConfigurationSettings.AppSettings["connectionString"];

Instead of getting the string value in c, I got null. I wonder whether this is because I cannot use AppSettings in a web service project. Any help is appreciated. Thanks.

Henry
 
Ok, it has nothing to do with web service project. I tried it on a web application project, and I have the same problem.

I suspect that the appsettings in the web.config is not read at all. Is there any way to verify this?

 
Try this System.Configuration.ConfigurationSettings.AppSettings["connectionString"]
 
Thanks for the reply, Henna.

I tried your suggestion but the problem still persist. System.Configuration.ConfigurationSettings.AppSettings["connectionString"] still return null, as if the key "connectionString" does not exist. In fact, ConfigurationSettings.AppSettings.Count returns 0.

Since I am not very familiar with IIS administration, I am thinking that there may be some misconfiguration with my IIS somehow.

These are what I have attempted so far ... I have double-checked C:\Inetpub\ to make sure that the <appSettings> stuff is there. I have also checked appSetting under machine.config to make sure that it's not commented out. So far, everything looks normal to me and I can't figure out what's wrong.

The problem is that there is no build error or runtime error that may give me a clue on what's going on. It is as if it either does not read the web.config at all or reads the "ghost" copy of the old web.config elsewhere. If you have other ideas, just let me know. Thanks.

Henry
 
Ok, I found the problem.

I am using NUnit to test my project and apparently System.Configuration.ConfigurationSettings.AppSettings does not work in standalone mode, such as for unit testing. That's why it doesn't read web.config for my test unit. So I guess I have to resort to manual XML parsing to read through the web.config for the unit testing.

Finally ... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top