Object reference not set to an instance of an object. - App.config

后端 未结 5 2010
南笙
南笙 2021-01-03 01:05

I receiving the error and in the local window I am seeing for both conSettings and connectionString value of null. I am right to say ConfigurationManager is null and I need

5条回答
  •  梦毁少年i
    2021-01-03 01:46

    you need to read AppSettings key as below ,

    string connectionString = 
          ConfigurationSettings.AppSettings["MyDBConnectionString"];
    

    still you receive empty value, try below steps

    1. Select the App.Config file in the solution explorer
    2. In the property window select Copy to Output Directory to Copy Always.
    3. Now Build the application and try again.

    to access like below you need to add connectionStrings section in app config

      string connectionString = 
          ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here
    

    sample app config

    
    
      
        
      
    
    

提交回复
热议问题