Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a value set in web config and I want to read it in my codes. But whenever I run it, it returns argumentnullvalue exception. I have previously successfully read the value for other key but it failed this time.
Below are my codes:
XML
<appSettings>
<add key="FlagWebAPI" value="true"/>
</appSettings>

C#
public bool FlagWebAPI 
        {
            get 
            {
                bool flag;
                flag = Boolean.Parse(ConfigurationManager.AppSettings["FlagWebAPI"]);

                return flag;
            }
            set { }
        }


What I have tried:

1. Tried to remove boolean parsing and and convert data type to string but getting the same error message.
Posted
Updated 20-Apr-16 23:53pm
v2
Comments
Karthik_Mahalingam 21-Apr-16 5:41am    
make sure you have added your key under app settings tag
could you post your web.config app settings part.

1 solution

Then you won't be putting the setting in the right place in the web.config, or there is something else about your set-up you're not telling us.

XML
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <appSettings>
    <add key="FlagWebAPI" value="true"/>
  </appSettings>
 
Share this answer
 
Comments
Jamie888 21-Apr-16 5:39am    
Sir, my setting is exactly the same as your answer above.
F-ES Sitecore 21-Apr-16 5:53am    
Then the code should work, as I said there is something about your set-up or project you're not telling us.
Jamie888 21-Apr-16 5:55am    
Sir, I have moved my value in web config file into app config file and it works now. But one thing that I am not sure, why does value in web config file doe snot work?
F-ES Sitecore 21-Apr-16 6:09am    
Because the web.config file is only used when your code is running in the context of a web site.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900