将Request.QueryString转换为整数

4 c# 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;
Run Code Online (Sandbox Code Playgroud)

Bar*_*Alp 9

int foo;
int.TryParse(Request.QueryString["foo"], out foo);
Run Code Online (Sandbox Code Playgroud)

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

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


Chr*_*Kee 6

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

<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>
Run Code Online (Sandbox Code Playgroud)