The name 'ConfigurationManager' does not exist in the current context

558,125

Solution 1

Ok.. it worked after restarting the VSTS. The link suggested the solution for the same problem. Wish i could have seen it before. :)

Solution 2

It's not only necessary to use the namespace System.Configuration. You have also to add the reference to the assembly System.Configuration.dll , by

  1. Right-click on the References / Dependencies
  2. Choose Add Reference
  3. Find and add System.Configuration.

This will work for sure. Also for the NameValueCollection you have to write:

using System.Collections.Specialized;

Solution 3

In your project, right-click, Add Reference..., in the .NET tab, find the System.Configuration component name and click OK.

using System.Configuration tells the compiler/IntelliSense to search in that namespace for any classes you use. Otherwise, you would have to use the full name (System.Configuration.ConfigurationManager) every time. But if you don't add the reference, that namespace/class will not be found anywhere.

Note that a DLL can have any namespace, so the file System.Configuration.dll could, in theory, have the namespace Some.Random.Name. For clarity/consistency they're usually the same, but there are exceptions.

Solution 4

Adding the System.Configuration as reference to all the projects will solve this.

  1. Go to Project -> Add Reference

  2. In the box that appears, click the All assemblies list tab in the left hand list.

  3. In the central list, scroll to System.Configuration and make sure the box is checked.

  4. Click ok to apply, and you'll now be able to access the ConfigurationManager class.

Solution 5

Adding this answer, as none of the suggested solutions works for me.

  1. Right-click on references tab to add reference.
  2. Click on Assemblies tab
  3. Search for 'System.Configuration'
  4. Click OK.
Share:
558,125
luoluuuo
Author by

luoluuuo

Just another curious nerd.

Updated on July 10, 2022

Comments

  • luoluuuo
    luoluuuo almost 2 years

    I am trying to access connectionStrings from the config file. The code is ASP.NET + C#. I have added System.Configuration to reference and also mentioned with using. But still it wouldn't accept the assembly.

    I am using VSTS 2008. Any idea what could be the reason?

    Another weird thing is the assembly name shown as "System.configuration", a lower case c which is not how names are displayed for other System assemblies.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace Utility
    {
        public class CommonVariables
        {
            public static String ConnectionString
            {
                get { return ConfigurationManager.ConnectionStrings["EmployeeEntities"].ConnectionString; }
            }  
        }  
    }
    

    Config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="qbankEntities" connectionString="metadata=res://*/qbankModel.csdl|res://*/qbankModel.ssdl|res://*/qbankModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
    </configuration>
    
  • luoluuuo
    luoluuuo almost 15 years
    Assembly details from property window: Name: System.configuration Path: C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.configu‌​ration.dll Version: 2.0.0.0 Runtime Version: v2.0.50727 Anything looks suspicious? Thanks!
  • Dan Diplo
    Dan Diplo almost 15 years
    No, that is exactly as expected. How really strange - I'm sorry, but I've never seen this before. Good luck!
  • Dhaust
    Dhaust almost 14 years
    +1 Thanks Kieran. Do you know why this has to be done when most other assemblies can simply be called by including the 'using' statement?
  • Kieran
    Kieran almost 14 years
    This is my understanding: it may be wrong. When you add the reference you are asking the dll to be copied to the bin folder on compile/ build. Some dll's seen as core are added when the project is created, ie when you go file->new project so the references are set up at that point. They all have to go through the same process just that some are done for you and some you have to do manually. You could test it out by deleting the reference to your System dll and watching all your code fail. =)
  • Kieran
    Kieran almost 14 years
    OK. So I have done a little more research and found that above is mostly true. However some of the files will not need to be written to the bin folder on run as they are in the Global Assembly cashe (GAC), where bin is local assembly cashe. Like the System.dll.
  • Reinstate Monica Cellio
    Reinstate Monica Cellio almost 8 years
    Over 7 years later and it's still a shame how often this is the answer.
  • jaybro
    jaybro over 7 years
    The 'System.Configuration' namespace can be found in 'System' dll, that's why there may be some head scratching when Intellisense accepts the 'using System.Configuration;' but will not find ConfigurationManager unless the System.Configuration.dll is referenced by the project.
  • ggiaquin16
    ggiaquin16 about 7 years
    nothing like marking your own reply as the answer, when it is indeed not the answer. Too bad mods can't edit it for the actual correct answer. Dude marked his own answer for points....
  • JAD
    JAD over 5 years
    @ggiaquin16 you don't get the points if you accept yourself ;)
  • ebyrob
    ebyrob over 4 years
    @calios or if you read the original question you can see that the questioner had already added the assembly to the project in the first version of the question. For this particular assembly and version of VS it was necessary to restart the IDE or the error would just keep happening. that was the solution. The two highly rated answers here were never relevant to the question actually being asked, but you don't think to wonder why someone would answer a question without reading it first. (in the vein of doing things just for points that does seem very effective in this case...)
  • Don Cheadle
    Don Cheadle almost 4 years
    why not use Nuget tho? Since you're likely already using Nuget to manage dependencies.
  • LordWabbit
    LordWabbit almost 4 years
    Why System.Configuration is not added as a reference in the default project template I don't know? Do so few people use the configuration file that it was decided you needed to add the reference manually to stop potential unnecessary reference bloat? I just find it very annoying, every project, "Add Reference".
  • oey
    oey over 3 years
    For dotnet core I had to run dotnet add package System.Configuration.ConfigurationManager in order to get this working.
  • khalidmehmoodawan
    khalidmehmoodawan almost 3 years
    VS .net could advice the same as a tooltip :-(
  • john ktejik
    john ktejik over 2 years
    dont forgot using System.Configuration;