在asp.net中将字符串类型转换为int

在asp.net中将字符串类型转换为int

问题描述:

我正在页面中使用会话.
在下一页中,我将其值转换为字符串变量,但在某个地方,我必须将其用作页面加载时fn()的int类型的整数
我该怎么做
我收到一个错误-输入字符串的格式不正确
告诉我所有可能的解决方案
请尽快帮助我...

I am using a session in my page.
in next page i am taking its value into string variable but at one place i have to use it as an int in argument of fn() at page load
how can i do that
i am getting an error- Input string was not in correct format
tell me all possible solutions
please help me soon...

好吧,我想该错误消息是很解释性的.如果您尝试转换空字符串或非整数的内容.

您仍然可以使用TryParse方法:

http://msdn.microsoft.com/en-us/library/system.int32. tryparse.aspx [ ^ ]
Well, I guess the error message is quite explanatory. If you try to convert an empty string or something which is not a integer.

You can use TryParse Method anyways:

http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx[^]


尝试一下

Try this

Session["intvalue"] = 2;
int i = int.Parse(Session["intvalue"].ToString());


在第二页中尝试以下操作:
Try this in your second page:
int output = 0;
if(Session["intvalue"] !=null)
{
    if(Session["intvalue"] != "")
    {
        int.tryParse(Session["intvalue"].ToString().Trim(), out output);
    }
}
//now output variable contains your integer value.


现在,将您的函数称为fn(output).


    -授予


Now call your function as fn(output).


     --Amit