C# 将 Request.QueryString 转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/548877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Request.QueryString to integer
提问by
How do i convert a Request.Query string to an integer value. I've tried all the Convert.ToInt32 and Int32.Parse but it says Input string is not in the correct format. I am using the string value as an input to a stored procedure which takes in only integer types for that field.
如何将 Request.Query 字符串转换为整数值。我已经尝试了所有 Convert.ToInt32 和 Int32.Parse,但它说输入字符串的格式不正确。我使用字符串值作为存储过程的输入,该存储过程只接受该字段的整数类型。
Here's a part of the code-
这是代码的一部分-
string rid=Request.QueryString["RID"];
lblRID.Text = rid;
int id= Int32.Parse(rid);
if (lblRID.Text!= null)
SqlCommand myCommand = new SqlCommand("usp_NewResource_get", myConnection);
myCommand.Parameters.AddWithValue("@RID",id); //RID is int in database and stored procedure
myCommand.CommandType = CommandType.StoredProcedure;
回答by Alex Reitbort
How about looking on the input that you provide to Int32.Parse?
查看您提供给 Int32.Parse 的输入怎么样?
回答by Barbaros Alp
int foo;
int.TryParse(Request.QueryString["foo"], out foo);
or just like you say, int.Parse
should convert to int
或者就像你说的,int.Parse
应该转换为 int
Could you post some code here ?
你能在这里发布一些代码吗?
回答by Ole Lynge
How about using TryParse like this:
像这样使用 TryParse 怎么样:
string rid = Request.QueryString["RID"]; int id = 0; if (!string.IsNullOrEmpty(rid) && Int32.TryParse(rid, out id)) { lblRID.Text = rid; SqlCommand myCommand = new SqlCommand("usp_NewResource_get", myConnection); myCommand.Parameters.AddWithValue("@RID",id); myCommand.CommandType = CommandType.StoredProcedure; myCommand.Execute... }
回答by JacquesB
The problem is that the value passed in the query string with the RID parameter is not a number, so it cant be parsed to an int. E.g you have ?RID=hello
or maybe no RID parameter at all (in which case the value is null). Inspect the querystring to see the actual value passed.
问题是带有 RID 参数的查询字符串中传递的值不是数字,因此无法解析为 int。例如,您有?RID=hello
或可能根本没有 RID 参数(在这种情况下,值为空)。检查查询字符串以查看传递的实际值。
回答by Chris McKee
Quick and dirty (and in a page load because this is an example, you should be able to work out what's happening from this)
快速而肮脏(并且在页面加载中,因为这是一个示例,您应该能够从中弄清楚发生了什么)
<script runat="server">
private void Page_Load(object sender, System.EventArgs e){
string test = Request.QueryString["foo"];
//Convert the query string you captured to an int and store it in an int.
int intTest = Convert.ToInt32(test);
Response.Write(intTest.GetType() + "<br>" + intTest);
}
</script>
回答by Rebecca
We use a base class from which every Page inherits. We have the following method that returns integer values from querystring params:
我们使用每个 Page 都继承的基类。我们有以下方法从查询字符串参数返回整数值:
protected int FetchQueryStringIdentifierByKey(string key)
{
int identifier;
var isInt = int.TryParse(Request.QueryString[key], out identifier);
return (isInt) ? identifier : 0;
}
Creditgoes to Jayson Knightfor his research into his original bench-marking results under .NET 2.0.
回答by Ravindran
string id = Request.QueryString["RID"].ToString();
userid=Convert.ToInt32(id);