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

pencilslate picture pencilslate · Aug 13, 2009 · Viewed 523.5k times · Source

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>

Answer

Kieran picture Kieran · Dec 3, 2009

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;