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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:45:14  来源:igfitidea点击:

Converting Request.QueryString to integer

c#request.querystring

提问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.Parseshould 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=helloor 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.

信用杰森骑士为他的研究到他原来的基准标记结果在.NET 2.0。

回答by Ravindran

string id = Request.QueryString["RID"].ToString(); 
userid=Convert.ToInt32(id);