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?
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
But if you want webconfig,then please see this same discussions
Reading a key from the Web.Config using ConfigurationManager
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.