0

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

Yong Shun
  • 35,286
  • 4
  • 24
  • 46

3 Answers3

1

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.

Iria
  • 43
  • 1
  • 10
1

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).

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
0

please set like this

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

this will work

doctorlove
  • 18,872
  • 2
  • 46
  • 62