Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configurationManager does not exist in the namespace System.Configuration

I have used the following namespace to connect my project to the sql server:

using System.Configuration;

and also used

string str=System.Configuration.ConfigurationSettings.AppSettings["myconnection"];
SqlConnection oconnection = new SqlConnection(str);
oconnection.Open();

when I run the program ,an error has been occurred and show the message

'System.Configuration.ConfigurationSettings.AppSettings' is obsolete.This method is obsolete,it has been replaced by 'System.Configuration! System.Configuration.ConfigurationManager.AppSettings '

but I have no found ConfigurationManager in that namespace and for oconnection.Open(); the message is

InvalidOperationException

was unhandled.

What can I do?

like image 374
Mohibullah Avatar asked Dec 18 '12 20:12

Mohibullah


People also ask

What is the namespace for ConfigurationManager in C#?

ConfigurationManager. AppeSettings["yadayada"];

What is ConfigurationManager configuration?

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.

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.


4 Answers

Go to references, and add a reference to System.Configuration

Once you have done this, you should be able to reference System.Configuration.ConfigurationManager.

string str = System.Configuration.ConfigurationManager.AppSettings["myconnection"];
SqlConnection oconnection = new SqlConnection(str);
oconnection.Open();

From MSDN: The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx


Edit: Addiitonal Information

In reference to the InvalidOperationException. This is caused when the connection string does not specify a data source or server. I am guessing that your connection string is empty.

In your web.config check the location of your connection string. If it falls under the element, then you will need to change your code to search ConnectionStrings and not AppSettings.

string str = System.Configuration.ConfigurationManager.
    ConnectionStrings["myconnection"].ConnectionString;
like image 187
Khan Avatar answered Oct 05 '22 12:10

Khan


If you have multiple projects in the solution you have to add the reference System.Configuration to each of them for ConfigurationManager to work in any of them.

like image 42
kschieck Avatar answered Oct 05 '22 11:10

kschieck


Add a reference of : System.Configuration.dll in your project, then ConfigurationManager will be availiable

like image 37
Amit K.S Avatar answered Oct 05 '22 10:10

Amit K.S


Install System.Configuration from nuget package. Then add reference.

like image 37
Pergin Sheni Avatar answered Oct 05 '22 11:10

Pergin Sheni