Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 597
pencilslate Avatar asked Aug 13 '09 21:08

pencilslate


People also ask

Does ConfigurationManager work in .NET core?

ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.

What is the use of ConfigurationManager in C#?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.

How do I add a reference to System configuration ConfigurationManager?

Right click the project and choose add reference, browse "Assemblies" and tick the System. Configuration assembly.


2 Answers

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; 
like image 117
Kieran Avatar answered Oct 06 '22 09:10

Kieran


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.

like image 44
Rameez Avatar answered Oct 06 '22 08:10

Rameez