Search code examples
asp.netconfiguration-management

How to fix "the name configurationmanager does not exist in the current context"


I already added the reference System.Configuration and still, I get recieving that error. I am using VS2010. Here is the code

<configuration>
<connectionStrings>
    <add name="MESConnection" connectionString="Server=.\SQLEXPRESS;Database=MES;Trusted_Connection=true"/>
</connectionStrings>
<system.web>
    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            <add assembly="System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
</system.web>

and the code behind:

private static string _connStr = ConfigurationManager.ConnectionStrings["MESConnection"].ConnectionString;

Any ideas?


Solution

  • Add a using statement at the top of your cs code file:

    using System.Configuration;
    

    Or fully qualify the reference:

    private static string _connStr = 
        System.Configuration.ConfigurationManager.ConnectionStrings["MESConnection"].ConnectionString;
    

    EDIT

    I see you haven't in fact referenced the correct assembly. You need to reference System.Configuration.dll not System.Configuration.Install.dll

    Also the usual way to reference a .NET assembly in Visual Studio is to right-click on the References node in Solution Explorer and choose Add Reference...

    enter image description here