4

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;
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111

7 Answers7

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

Raj Baral
  • 661
  • 6
  • 19
Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61
  • string rid=Request.QueryString["RID"]; lblRID.Text = rid; int id= Int32.Parse(rid); I've tried that and string rid=Request.QueryString["RID"]; lblRID.Text = rid; int id= Convert.ToInt32(lblRID.Text) or int id= Convert.ToInt32(rid) –  Feb 14 '09 at 10:38
  • HeadScratcher. Could you update your original post putting the code in a codeblock so we can read it easier. I think Barbaros is right with his TryParse, you might also want to look at string.IsNullOrEmpty to check the contents of the query string – Greg B Feb 14 '09 at 10:56
  • i tried tryparse..Using TryParse executes d statements after the if statement correctly(button visibilty executes perfectly after the if) but im using RID(which is int) in the WHERE clause in a stored procedure to read fields from a table.. and using tryparse just does not convert querystring to int –  Feb 14 '09 at 11:03
7

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>
Chris McKee
  • 4,298
  • 10
  • 48
  • 83
3

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

Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
  • Ok.. Im passing a request ID from a page in a gridview to another page and i've given the - string rid=Request.QueryString["RID"] on the page load.. Now i need to display this string in a label on the page and then convert it into an integer so that i can use it as an input to a stored procedure.. –  Feb 14 '09 at 10:31
  • look in debugger what you have inside rid variable. – Alex Reitbort Feb 14 '09 at 10:34
  • string rid=Request.QueryString["RID"]; lblRID.Text = rid; int id= Int32.Parse(rid); ---->Shows input string not in a correct format error here if (lblRID.Text!= null) Statements after this if are executing normally –  Feb 14 '09 at 10:43
  • what is the VALUE of variable rid? What is displayed on the label? – Alex Reitbort Feb 14 '09 at 11:11
  • the label displays integer values.. RID is an integer –  Feb 14 '09 at 11:13
  • CAN YOU POST THE VALUE HERE PLEASE?!?!?!? – Alex Reitbort Feb 14 '09 at 11:36
2

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.

Rebecca
  • 13,914
  • 10
  • 95
  • 136
1
string id = Request.QueryString["RID"].ToString(); 
userid=Convert.ToInt32(id);
Ravindran
  • 37
  • 10
0

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...
}
Ole Lynge
  • 4,457
  • 8
  • 43
  • 57
0

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.

JacquesB
  • 41,662
  • 13
  • 71
  • 86