Question

I am trying to create a function that can return the corresponding value from web.config based on the key provided

For instance, I have these in my Web.config file:

<Records>
    <add key="0" value="123cb456" />
    <add key="1" value="hFh343" />
    <add key="2" value="Hdkr625" />
    <add key="3" value="1732HHds" />
    <add key="4" value="optxy33" />
 </Records>

I am trying to create a function that can return the corresponding record value based on the key provided .Something like the following

 public static string GetSelectedRecordValue(string strkey)
 {
      string strValue;
      foreach (KeyValueConfigurationElement item in Configuration.Settings("Records")
      {
           if (item.Key == strKey)
           {
                strValue = item.Value;
                return strValue;
           }
      }
      return strValue;
 }

How to get the contents of my config section into a collection so that I can loop through each item and read correct value record based on a key ?any suggestions?

Was it helpful?

Solution

Why You add this colors in webconfig? You can set these values in constant class . and you can get easily .

Please refer the below websites

Link 1

Link 2

But if you want webconfig,then please see this same discussions

Reading a key from the Web.Config using ConfigurationManager

How to read values from custom section in web.config

OTHER TIPS

I think you can try WebConfigurationManager.OpenWebConfiguration : http://msdn.microsoft.com/en-us/library/vstudio/system.web.configuration.webconfigurationmanager.openwebconfiguration(v=vs.90).aspx

or just read the web.config as a normal xml file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top