Object reference not set to an instance of an object in ASP.NET
This question already has an answer here:
Problem : You might be missing ConnectionString SchoolConnectionString1
in your web.config
file.
Solution : You need to add ConnectionString
in your web.config
file as below:
<connectionStrings>
<add
name="SchoolConnectionString1"
connectionString="Data Source=serverName;Initial
Catalog=DatabaseName;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
Please check if you actually have a "SchoolConnectionString1" connection string in your config file.
In the config file (web.config if you are working on a web project), under you should see something like this:
<add name="SchoolConnectionString1" connectionString="Data Source=someServer;Initial Catalog=MyDB[...]" providerName="System.Data.SqlClient" />
If this is not the case, and the connection string appears correct, check if your web.config as a whole is well-formatted.
Most propably this is a case of misplaced web config or invalid structure (if the connection string is there and there is no spelling error in
SchoolConnectionString1
as stated in the other answers.
In our projects there are more than one Web config files (one for debug and one for release) so this stuff happens regularly.
So check if there are two in your case (maybe your project has Web.Degug.Config and Web.Release.Config for different builds). This is an option in VS 2012 (Maybe earlier versions too) to replace some of your configuration for different enviroments.
链接地址: http://www.djcxy.com/p/28050.html