Mark Jordan
Mark Jordan

Reputation: 37

Unable to get ConnectionString value from Configuration

I do not know why am I getting this error I have tried everything but I am still getting this error again and again. Any help which would solve this problem will be appreciated.

Here is the error:

enter image description here

Connection string in web.config:

enter image description here

Upvotes: 0

Views: 919

Answers (3)

Yong Shun
Yong Shun

Reputation: 51450

As the extension to the solution which I mentioned in the question's comment:

EXPLANATION

According to the article Tale of two web.config in MVC,

web.config in View folder

Implemented for blocking access to view folder and files directly via user request or through URL.

Web.config in project folder

Where all the appSettings, connectionStrings, configSections and etc. are configured here and these settings/values are able to be accessible for the entire project.


SOLUTION

Thus, you need to move the <ConnectionStrings> section from web.config (in the root of the View folder) to Web.config (in the root of the project folder).

Upvotes: 1

Neha Sojitra
Neha Sojitra

Reputation: 1

please set like this

string ConnectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"];

this will work

Upvotes: 0

Iria
Iria

Reputation: 43

I see several issues, but the main one is that you are getting null, why are you getting null? Because that's what you get after reading your settings file. So basically, the problem is that you are reading null from settings file, if you are passing correctly the configuration, that is, the settings file.

That's what I would do,

When debugging, see the Configuration Manager, try to see what you have attached to it and it's values, then, it is easy to figure out what it should be.

I think your problem is in the ConfigurationManager, in fact, you should use more the breakpoints and the watch.

Have a look at Read a connection string from a XML file, to see how it should be done.

Upvotes: 1

Related Questions