I have defined the following inside my web.config file:-
<add key="TechPageSize" value="20" />
But I m unable to reference this value inside my paging parameters as follow:-
var servers = repository.AllFindServers(withOutSpace).OrderBy(a => a.Technology.Tag).ToPagedList(page, (Int32)System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);
and I will get an error that it can not change String to INT.
Any idea what is the problem ?
int techPageSize;
if (!int.TryParse(ConfigurationManager.AppSettings["TechPageSize"], out techPageSize))
{
throw new InvalidOperationException("Invalid TechPageSize in web.config");
}
Int32.TryParse
has two effects:
techPageSize
, if possible.False
, allowing you to handle the error as you see fit.PS: It suffices to use ConfigurationManager.AppSettings
, once you have imported the System.Configuration
namespace.
The result of
System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]
is a string, you can't just cast a string to int. Use Convert.ToInt32 or int.TryParse to get the integer value of the string.
int pagesize = Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);
or
bool succeed = int.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"], out pagesize);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With