将 Request.QueryString 转换为整数

发布于 2024-07-13 20:03:53 字数 531 浏览 11 评论 0原文

如何将 Request.Query 字符串转换为整数值。 我已经尝试了所有 Convert.ToInt32 和 Int32.Parse 但它说输入字符串的格式不正确。 我使用字符串值作为存储过程的输入,该存储过程仅接受该字段的整数类型。

这是代码的一部分 -

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;

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.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

画尸师 2024-07-20 20:03:53
int foo;
int.TryParse(Request.QueryString["foo"], out foo);

或者就像你说的, int.Parse 应该转换为 int

你可以在这里发布一些代码吗?

int foo;
int.TryParse(Request.QueryString["foo"], out foo);

or just like you say, int.Parse should convert to int

Could you post some code here ?

停顿的约定 2024-07-20 20:03:53

如今,答案因您使用的框架而异,因为 msft 已将查询参数作为视图属性模型的一部分,用于绑定(参考:https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.fromqueryattribute? view=aspnetcore-3.1)。

举例来说,您仍然可以通过 httpcontext 访问大多数内容。

var fooIsInt = int.TryParse(HttpContext.Request.Query["foo"], out var foo);

.net 2.0 中 Web 表单的原始示例

快速且肮脏(并且在页面加载中,因为这是一个例子,你应该能够弄清楚发生了什么)

<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>

The answer thesedays varies based on what framework you're using as msft has made query params part of the view attribute model now for binding (ref: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.fromqueryattribute?view=aspnetcore-3.1).

You can still access most things via httpcontext for the sake of example.

var fooIsInt = int.TryParse(HttpContext.Request.Query["foo"], out var foo);

Original Example for webforms in .net 2.0

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>
想念有你 2024-07-20 20:03:53

查看您提供给 Int32.Parse 的输入怎么样?

How about looking on the input that you provide to Int32.Parse?

小忆控 2024-07-20 20:03:53

我们使用每个 Page 都继承自的基类。 我们有以下方法从查询字符串参数返回整数值:

protected int FetchQueryStringIdentifierByKey(string key)
{
    int identifier;
    var isInt = int.TryParse(Request.QueryString[key], out identifier);
    return (isInt) ? identifier : 0;
}

归功于 Jayson Knight 对 下原始基准测试结果的研究。 NET 2.0。

We use a base class from which every Page inherits. We have the following method that returns integer values from querystring params:

protected int FetchQueryStringIdentifierByKey(string key)
{
    int identifier;
    var isInt = int.TryParse(Request.QueryString[key], out identifier);
    return (isInt) ? identifier : 0;
}

Credit goes to Jayson Knight for his research into his original bench-marking results under .NET 2.0.

巴黎夜雨 2024-07-20 20:03:53
string id = Request.QueryString["RID"].ToString(); 
userid=Convert.ToInt32(id);
string id = Request.QueryString["RID"].ToString(); 
userid=Convert.ToInt32(id);
歌入人心 2024-07-20 20:03:53

像这样使用 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...
}

How about using TryParse like this:

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...
}
记忆消瘦 2024-07-20 20:03:53

问题在于,使用 RID 参数传入查询字符串的值不是数字,因此无法将其解析为 int。 例如,您有 ?RID=hello 或者根本没有 RID 参数(在这种情况下该值为 null)。 检查查询字符串以查看传递的实际值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文