Object reference not set to an instance of an object in ASP.NET

This question already has an answer here:

  • What is a NullReferenceException, and how do I fix it? 33 answers

  • 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

    上一篇: 为什么这个结构是可变的? 什么时候可变结构可以接受?

    下一篇: 对象引用未设置为ASP.NET中对象的实例