[c#] The name 'ConfigurationManager' does not exist in the current context

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>

This question is related to c# .net visual-studio visual-studio-2008

The answer is


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.


Are you sure you have added a reference to the .NET assembly and not something else? I'd remove your reference and then try re-adding it, making sure you select from the .NET tab in Visual Studio reference dialogue - the latest version should be 2.0.0.0 in GAC.


For a sanity check, try creating a new Web Application Project, open the code behind for the Default.aspx page. Add a line in Page_Load to access your connection string.

It should have System.Configuration added as reference by default. You should also see the using statement at the top of your code file already.

My code behind file now looks like this and compiles with no problems.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication1
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string connString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString;
    }
  }
}

This assumes I have a connection string in my web.config with a name equal to "MyConnectionStringName" like so...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="MyConnectionStringName"
            connectionString="Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Yeah, it's elementary I know. But if you don't have any better ideas sometimes it helps to check against something really simple that you know should work.


You may also get this error if you add a reference to a different, unrelated project by mistake. Check if that applies to you.


If you're getting a lot of warnings (in my case 64 in a solution!) like

CS0618: 'ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

because you're upgrading an older project you can save a lot of time as follows:

  1. Add System.Configuration as a reference to your References section.
  2. Add the following two using statements to the top of each class (.cs) file:

    using System.Configuration; using ConfigurationSettings = System.Configuration.ConfigurationManager;

By this change all occurances of

ConfigurationSettings.AppSettings["mySetting"]

will now reference the right configuration manager, no longer the deprecated one, and all the CS0618 warnings will go away immediately.

Of course, keep in mind that this is a quick hack. On the long term, you should consider refactoring the code.


I have gotten a better solution for the issue configurationmanager does not exist in the current context.

To a read connection string from web.config we need to use ConfigurationManager class and its method. If you want to use you need to add namespace using System.Configuration;

Though you used this namespace, when you try to use the ConfigurationManager class then the system shows an error “configurationmanager does not exist in the current context”. To solve this Problem:

ConfigurationManager.ConnectionStrings["ConnectionSql"].ConnectionString; 

Just install

Install-Package System.Configuration.ConfigurationManager -Version 4.5.0

then use

using System.Configuration;

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.


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.

The configuration manager of web configuration (web.config) is obsolete. So see this https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.appsettings?redirectedfrom=MSDN&view=dotnet-plat-ext-3.1#System_Configuration_ConfigurationManager_AppSettings

create a instance of settings

var appSettings = ConfigurationManager.AppSettings;

In NetCore 3.1 I had to use Microsoft.Extensions.Configuration instead:

    public static async Task Main(string[] args)
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
            .Build();

...


If this code is on a separate project, like a library project. Don't forgeet to add reference to system.configuration.


  1. Right-click on Project
  2. Select Manager NuGet Package
  3. Find System.Configuration
  4. Select System.Configuration.ConfigurationManager by Microsoft
  5. Install

now you can:

using System.Configuration;

Add the configuration manager file to connect to the database web.config


To Solve this problem go to Solution Explorer And right click reference and click add reference and chose .net and find system.configuration select an click ok


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;

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references

Examples related to visual-studio-2008

Convert Text to Uppercase while typing in Text box SQL Server r2 installation error .. update Visual Studio 2008 to SP1 What is and how to fix System.TypeInitializationException error? Error LNK2019: Unresolved External Symbol in Visual Studio download and install visual studio 2008 Git in Visual Studio - add existing project? Visual Studio can't 'see' my included header files How to insert Records in Database using C# language? If statements for Checkboxes LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup