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="Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
If this code is on a separate project, like a library project. Don't forgeet to add reference to system.configuration.
Adding the
System.Configuration
as reference to all the projects will solve this.Go to
Project
->Add Reference
In the box that appears, click the
All assemblies
list tab in the left hand list.In the central list, scroll to
System.Configuration
and make sure the box is checked.Click ok to apply, and you'll now be able to access the
ConfigurationManager
class.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 namespaceSome.Random.Name
. For clarity/consistency they're usually the same, but there are exceptions.If you're getting a lot of warnings (in my case 64 in a solution!) like
because you're upgrading an older project you can save a lot of time as follows:
System.Configuration
as a reference to your References section.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
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.
It's not only necessary to use the namespace
System.Configuration
. You have also to add the reference to the assemblySystem.Configuration.dll
, bySystem.Configuration
.This will work for sure. Also for the
NameValueCollection
you have to write:Adding this answer, as none of the suggested solutions works for me.