将 Request.QueryString 转换为整数
如何将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
或者就像你说的,
int.Parse
应该转换为 int你可以在这里发布一些代码吗?
or just like you say,
int.Parse
should convert to intCould you post some code here ?
如今,答案因您使用的框架而异,因为 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 表单的原始示例
快速且肮脏(并且在页面加载中,因为这是一个例子,你应该能够弄清楚发生了什么)
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)
查看您提供给 Int32.Parse 的输入怎么样?
How about looking on the input that you provide to Int32.Parse?
我们使用每个 Page 都继承自的基类。 我们有以下方法从查询字符串参数返回整数值:
归功于 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:
Credit goes to Jayson Knight for his research into his original bench-marking results under .NET 2.0.
像这样使用 TryParse 怎么样:
How about using TryParse like this:
问题在于,使用 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.