Retrieve AppSettings in HTML

I'm trying to set the text of a label on a webform equal to the value of a "key" in Web.Config. Here's the html code that returns an error saying "Operand ConfigurationManager is not declared":

<body>

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text='<%# ConfigurationManager.AppSettings["KeyName"] %>'></asp:Label>

</div>

</form>

</body>

 

 

And it's trying to retrieve the value from Web.Config (very similar to the Micro Focus example solution BookDemoWebApplication):

 

<?xml version="1.0"?>

<configuration>

<configSections>

<sectionGroup name="MicroFocus.COBOL.Application">

<section name="Environment" type="System.Configuration.NameValueSectionHandler" />

</sectionGroup>

</configSections>

<MicroFocus.COBOL.Application>

<Environment>

<add key="KeyName" value="KeyValue"/>

</Environment>

</MicroFocus.COBOL.Application>

<system.web>

<compilation batch="false" debug="false" targetFramework="4.0" />

</system.web>

<system.webServer>

<modules runAllManagedModulesForAllRequests="true"/>

</system.webServer>

<system.codedom>

<compilers>

<compiler language="COBOL" extension=".cbl" compilerOptions="sourceformat(variable) warnings(2)" type="MicroFocus.COBOL.CodeDom.COBOLCodeProvider, MicroFocus.COBOL.CodeDom, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0412c5e0b2aaa8f0"/>

</compilers>

</system.codedom>

</configuration>

 

  • The section in web.config in which you are setting this is for setting environment variables and not strings.

    You should add an appsettings section under the configuration header and read the value from there:

    <appsettings>
        <add value="KeyValue" key="KeyName"></add>
      </appsettings>

    and then you can set this either within the html or within code-behind code:

    html

    <asp:Label ID="Label1" runat="server" Text="<%$ AppSettings:KeyName%>"></asp:Label>

    or code behind

     

              declare config as type System.Configuration.Configuration
              set config to type WebConfigurationManager::OpenWebConfiguration("~/")
              
              if config::AppSettings::Settings::Count > 0
                 set Label1::Text to config::AppSettings::Settings["KeyName"]::Value 
              else
                 set Label1::Text to ""
              end-if
    
  • Thanks, Chris! I'm not quite there, but you've definitely helped me get closer. Any idea where I'm going wrong?

    Here's the error message: The application setting 'KeyName' was not found in the applications configuration.

    Just to make sure my problem is clear, I'm looking to hardcode a value in web.config that can be retrieved dynamically in the html of the webform. This will allow me to have many webforms in the solution and only have to change a value once in web.config for it to change on all the webforms.

    Here's the updated code:

    Web.Config:

    <?xml version="1.0"?>

    <configuration>

     <configSections>

       <sectionGroup name="MicroFocus.COBOL.Application">

         <section name="AppSettings" type="System.Configuration.NameValueSectionHandler"/>

       </sectionGroup>

     </configSections>

     <MicroFocus.COBOL.Application>

       <AppSettings>

         <add key="KeyName" value="KeyValue"/>

       </AppSettings>

     </MicroFocus.COBOL.Application>

     <system.web>

       <compilation batch="false" debug="true" targetFramework="4.0"/>

     </system.web>

     <system.webServer>

       <modules runAllManagedModulesForAllRequests="true"/>

     </system.webServer>

     <system.codedom>

       <compilers>

         <compiler language="COBOL" extension=".cbl" compilerOptions="sourceformat(variable) warnings(2)" type="MicroFocus.COBOL.CodeDom.COBOLCodeProvider, MicroFocus.COBOL.CodeDom, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0412c5e0b2aaa8f0"/>

       </compilers>

     </system.codedom>

    </configuration>

    ASP Webform:

    <asp:Label ID="Label1" runat="server" Text="<%$ AppSettings:KeyName%>"></asp:Label>

  • Verified Answer

    AppSettings is a predefined section in web.config so it shouldn't be redefined under the MicroFocus section.

    The complete web.config that I am using in my example is:

    <?xml version="1.0"?>

    <configuration>

     <appSettings>

       <add key="KeyName" value="KeyValue" />

     </appSettings>

     <system.web>

       <compilation batch="false" debug="true" targetFramework="4.0" />

       <authentication mode="Forms">

         <forms loginUrl="~/Account/Login.aspx" timeout="2880" />

       </authentication>

       <membership>

         <providers>

           <clear />

           <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />

         </providers>

       </membership>

       <profile>

         <providers>

           <clear />

           <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />

         </providers>

       </profile>

       <roleManager enabled="false">

         <providers>

           <clear />

           <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />

           <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />

         </providers>

       </roleManager>

     </system.web>

     <system.webServer>

       <modules runAllManagedModulesForAllRequests="true" />

     </system.webServer>

     <system.codedom>

       <compilers>

         <compiler language="COBOL" extension=".cbl" compilerOptions="sourceformat(variable) warnings(2)" type="MicroFocus.COBOL.CodeDom.COBOLCodeProvider, MicroFocus.COBOL.CodeDom, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0412c5e0b2aaa8f0" />

       </compilers>

     </system.codedom>

    </configuration>