在这个程序错误输入字符串不正确的请求解决此问题 [英] In this program error input string was not in correct fora pls solve this problem
问题描述
protected void Page_Load(object sender, EventArgs e)
{
string score = Request.QueryString["Score"];
Label1.Text = score;
int value = Convert.ToInt32(score);
if (value >= 10)
{
Label2.Text = "Cheers ! your score is satisfactory to promote you for the next level. All the best for the Next Move.";
}
else
{
Label2.Text = "Sorry ! Your score is not satisfactory to promote you for the next level. You can again attempt this level after Seven Days.";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int level = Convert.ToInt32(Request.QueryString["level"]);
Response.Redirect("Toplist.aspx?level=" + level);
}
}
输入字符串格式不正确
Input string was not in correct format
推荐答案
停止使用转换
方法来更改数据类型:特别是当您从字符串转换为数字时,您需要使用TryParse
。转换方法在失败时抛出异常,其中TryParse返回一个值,允许您报告问题而不是您的应用程序(和网站)失败。
Stop usingConvert
methods to change between datatypes: particularly when you are converting from strings to numbers you need to useTryParse
instead. Convert methods throw an exception when they fail, where TryParse returns a value which allows you to report problems instead of your app (and website) failing.
string score = Request.QueryString["Score"];
Label1.Text = score;
int value;
if (!int32.TryParse(score, out value))
{
// Report problem, or log it.
...
return;
}
但是它失败的原因很可能出现在指向此页面的代码中:使用上面的if来显示查询字符串包含的内容,然后看它应该是什么 - 因为你正在生成查询字符串,你可能在那里做了非常非常错误的事情。
作为旁注:如果您不希望用户在得分足够之前继续前进,那么您需要查看使用Cookie或Session对象而不是查询字符串。否则,只需在浏览器顶部的URL中编辑查询字符串并按ENTER键即可立即进行...
But the reason why it's failing is very likely to be in the code which directed you to this page: use the if above to show you what exactly the query string contains, and then look at what it should be - since you are generating the query string, you are probably doing something very, very wrong there.
As a side note: if you don't want users to progress until they have a sufficient score, then you need to look at using Cookies or the Session object instead of query strings. Otherwise I can progress immediately just by editing the query string in the URL at the top of my browser and pressing ENTER...
这篇关于在这个程序错误输入字符串不正确的请求解决此问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!