Converting Request.QueryString to integer
Asked Answered
D

7

5

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;
Dianadiandra answered 14/2, 2009 at 10:15 Comment(2)
suggest you post some code...Thi
Is this C# or something else? Please tag appropriately...Footslog
R
11
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 ?

Rugen answered 14/2, 2009 at 10:32 Comment(3)
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)Dianadiandra
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 stringTrinl
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 intDianadiandra
M
8

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>
Melisamelisande answered 17/2, 2009 at 13:50 Comment(0)
F
3

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

Fleabag answered 14/2, 2009 at 10:25 Comment(6)
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..Dianadiandra
look in debugger what you have inside rid variable.Fleabag
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 normallyDianadiandra
what is the VALUE of variable rid? What is displayed on the label?Fleabag
the label displays integer values.. RID is an integerDianadiandra
CAN YOU POST THE VALUE HERE PLEASE?!?!?!?Fleabag
S
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.

Squeeze answered 14/11, 2011 at 12:24 Comment(0)
Z
1
string id = Request.QueryString["RID"].ToString(); 
userid=Convert.ToInt32(id);
Zephyrus answered 8/11, 2014 at 3:52 Comment(1)
Your answer should contain an explanation of your code and a description how it solves the problem.Railey
H
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...
}
Hyperopia answered 14/2, 2009 at 11:41 Comment(0)
S
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.

Scarlatti answered 14/2, 2009 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.