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>
ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.
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.
Right click the project and choose add reference, browse "Assemblies" and tick the System. Configuration assembly.
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
System.Configuration
. This will work for sure. Also for the NameValueCollection
you have to write:
using System.Collections.Specialized;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With