1

Good evening people, im working on a little project and the ConfigurationSection i wrote is not returning the data i put in in the web.config.

enter code here

my code:

public class AdminSection : ConfigurationSection
    {
        private static AdminSection uniqueInstance;

    public static AdminSection Instance
    {
        get { return uniqueInstance ?? (uniqueInstance = new AdminSection()); }
    }


    private AdminSection()
    {          
    }

    [ConfigurationProperty("Username", IsRequired =true)]
    public  String Username
    {
        get { return (String)this["Username"]; }
    }

    [ConfigurationProperty("Password",  IsRequired = false)]
    public String Password
    {
        get { return (String)this["Password"]; }
    }
}

here is my web.config

<configuration>
  <configSections>
    <section name="Admin" type="cms.Configs.AdminSection, cms.cms"/>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
  <Admin>
    <Username>test</Username>
    <Password>test2</Password>
  </Admin>
</configuration>

When i trie to call the following

Username.Value == Configs.AdminSection.Instance.Username

Im not getting any values from the AdminSection.

Does any of you have a single clue of what i might be doing wrong?

Mickboe1
  • 45
  • 1
  • 7
  • have you referenced the msdn documentation on how to do this [MSDN ConfigurationSection Class](https://msdn.microsoft.com/en-us/library/system.configuration.configurationsection%28v=vs.110%29.aspx) – MethodMan Feb 05 '15 at 22:13
  • maybe configuration manager is unable to instantiate the class ? did You try changing constructor from Private to Public ? – Marty Feb 05 '15 at 22:17
  • Yes i have tried to make it public, ive also tried not using the instance but the following :AdminSection test = new AdminSection(); if (Username.Value == test.Username && UserPass.Value == test.Password), – Mickboe1 Feb 05 '15 at 22:21
  • found the awnser i forgot to do the following : ConfigurationManager.GetSection("Admin") as AdminSection; – Mickboe1 Feb 06 '15 at 08:12

1 Answers1

0

found the awnser i forgot to do the following :

public AdminSection Admin

{
    get
    {
        try
        {
            if (_instance == null)
            {
                _instance = ConfigurationManager.GetSection("Admin") as AdminSection;   
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return _instance;
    }
}
Mickboe1
  • 45
  • 1
  • 7