How to convert System.Web.Configuration.WebConfigurationManager.AppSettings from String to INT

asp.net-mvc, c#

Solution

int techPageSize;
if (!int.TryParse(ConfigurationManager.AppSettings["TechPageSize"], out techPageSize))
{
    throw new InvalidOperationException("Invalid TechPageSize in web.config");
}

`Int32.TryParse` has two effects:

- It converts the app setting to an integer and stores the result in `techPageSize`, if possible.

- If the value cannot be converted, the method returns `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.

Problem

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 ?

Original source