views:

122

answers:

2

I want to access the "appSettings" section of a web.config file. According to MSDN and multiple other sources, the following should work:

System.Configuration.Configuration config = 
    WebConfigurationManager.OpenWebConfiguration("/rootPath");

I received this error on the second configuration (System.Configuration.Configuration):

The type or namespace name 'Configuration' does not exist in the namespace 'System.Configuration'(are you missing an assembly reference?)

I received an additional error on system (System.Configuration.Configuration):

The type 'System.Configuration.Configuration' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Configuration, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Addressing the first error, I have a reference to System.Configuration. When I look in Object Browser, it contains a class definition for Configuration.

Addressing the second error, the reference is for version 2.0.50727. Is there a version 2.0.0.0?

The second error popped up after I deleted, and re-added the reference to System.Configuration. Also, after re-adding the reference it now displays as "System.configuration" in my references. Note the lowercase c.

Additional Notes
Things I've tried and failed:

  • Deleted and added the reference to System.Configuration
  • Deleted all references for the project and added them back
  • Checked out a clean solution from SVN

I created a test solution and had no problems accessing the class in the System.Configuration namespace.

Any ideas on what is going on (how did I eff this up)? What avenues can I try to fix it?

+1  A: 

Try to remove that references to System.Configuration and do it from scratch: you should to add version=2.0.0.0 (for runtime version v2.0.50727). Then, to read your appSetting, you could just write this:

string value = System.Configuration.ConfigurationManager.AppSettings["yourKey"];
Rubens Farias
Yes, but I'm curious why it's looking for that version. My Framework folder doesn't contain a version 2.0, just 1.0.3705, 1.1, 2.0.50727, 3.0, and 3.5. So I'll have download from MSDN. This seems like an atypical step, like something must be messed up with my references.
Becky
That is the first thing I tried. When I added it back, it displayed with the the lowercase c: "System.configuration", and displayed the second error mentioned.
Becky
That lowercase `c` is "right": I got this here too; but I didnt understood your second error, can you please clarify it?
Rubens Farias
The second error was "The type 'System.Configuration.Configuration' is defined in an assembly that is not referenced...". As a sanity check I created a quick project and had no problem accessing the classes in the System.Configuration namespace (including Configuration and ConfigurationManager). So it's definitely a wonky issue with that project's references, I'm just unclear on how fix it. I'll try removing all of my references and adding them back one by one.
Becky
A: 

This is so stupid, I want to delete this question and quietly back away. Perhaps it'll be useful to some other fool. Eilon was on the right track, I was adding the reference to the wrong project.

I was in a time crunch trying to get a project production ready and was not very focused (trying to do several things at once). Ah well, I'm still new to this and hopefully next time I will take a deep breath and not stress out...and start the project out with connection strings stored in the web config.

Becky