locked
My code is not reading from web.config RRS feed

  • Question

  • User1216627406 posted

    Sorry for another thread.

    I have the following that is supposed to read from web.config file.

      public string ReadConfig(string ValueToGet)
            {
                System.Configuration.AppSettingsSection section = (System.Configuration.AppSettingsSection)config.GetSection("appSettings");
                return section.Settings[ValueToGet].Value;
            }
            
            // get the report URL
            string URL = Master.ReadConfig("DailyReport");

    The application runs perfectly on my local machine but again when run from the server, it gives the following error:

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Any idea how to fix this?

    Thanks again in advance

    Friday, December 11, 2020 6:38 PM

Answers

  • User807886609 posted

    Typically it's not necessary to make things this complicated. A simple way would be like this:

    string DailyReportUrl = ConfigurationManager.AppSettings["DailyReportUrl"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 11, 2020 9:27 PM

All replies

  • User475983607 posted

    The "config" variable is not defined or populated anywhere in the code you shared.   Where ever you are populating the variable is where the problem starts.  Use the Visual Studio debugger to step through the code.

    Friday, December 11, 2020 8:42 PM
  • User1216627406 posted

    Thanks mgebhard.

    The issue though as I stated is that this works perfectly on my local PC.

    So, how do you debug something that is working?

    At the top of the page, just below partial class... is where it is defined:

           Configuration config = WebConfigurationManager.OpenWebConfiguration("/");

    Friday, December 11, 2020 9:16 PM
  • User807886609 posted

    Typically it's not necessary to make things this complicated. A simple way would be like this:

    string DailyReportUrl = ConfigurationManager.AppSettings["DailyReportUrl"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 11, 2020 9:27 PM
  • User1216627406 posted

    Sorry sir but what line is your code replacing?

    Friday, December 11, 2020 9:41 PM
  • User475983607 posted

    The issue though as I stated is that this works perfectly on my local PC.

    Every novice developers makes this point but fails to realize that the code is running on an another systems which is not the same as the dev machine.   

    So, how do you debug something that is working?

    I don't understand your response.  The code is not working and throws a null exception.  The virtual path on the server is probably different or maybe access rights.

    I agree with the previous poster.  Use standard configuration libraries for accessing web.config settings.

    https://stackoverflow.com/questions/8147220/how-to-read-appsettings-section-in-the-web-config-file

    You can replace you few line of code with the one.

    Friday, December 11, 2020 9:45 PM
  • User1216627406 posted

            Every novice developers makes this point but fails to realize that the code is running on an another systems which is not the same as the dev machine.  
        
        Right, I get that sir but how do you debug an application that is working correctly on your system?
        
        It only fails when you deploy it to the server.
        
        You attempt to debug it on your system where it is working correctly and it shows the correct value. How does that help you fix the issue on the server?
        
        Not sure if it is an entirely related to being a novice.
        
    About this, You can replace you few line of code with the one.

    Did he mean, replace this:

    string URL = Master.ReadConfig("DailyReport");

    with this:

    string URL = ConfigurationManager.AppSettings["http://server/ReportServer?/Reports/DailyReport"];

    ???

    Friday, December 11, 2020 9:58 PM
  • User475983607 posted

          Every novice developers makes this point but fails to realize that the code is running on an another systems which is not the same as the dev machine.  
        
        Right, I get that sir but how do you debug an application that is working correctly on your system?
        
        It only fails when you deploy it to the server.
        
        You attempt to debug it on your system where it is working correctly and it shows the correct value. How does that help you fix the issue on the server?
        
        Not sure if it is an entirely related to being a novice.

    Basic troubleshooting.  You know the code works on the dev machine but not on the server.  You know the line of code that caused the exception from the error message.  The line of code works in the dev box but not the remote machine.  Since you are using a virtual path, that is probably where the problem starts.  

    Secondly, you could simply log the variable state.  

    string URL = Master.ReadConfig("DailyReport");

    with this:

    string URL = ConfigurationManager.AppSettings["http://server/ReportServer?/Reports/DailyReport"];

    ???

    Not exactly.  According you your code the  appSettings key is "DailyReport".   So the syntax is...

    string URL = ConfigurationManager.AppSettings["DailyReport"];

    I cannot see the appSetting so I can't be sure.  The link I provided above illustrates how the ConfigurationManager works.

    Friday, December 11, 2020 10:21 PM
  • User1216627406 posted

    Thank you sir.

    Your solution worked.

    Saturday, December 12, 2020 1:07 AM