HeadScratcher
HeadScratcher

Reputation:

Converting Request.QueryString to integer

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;

Upvotes: 5

Views: 48244

Answers (7)

Chris McKee
Chris McKee

Reputation: 4387

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>

Upvotes: 8

Barbaros Alp
Barbaros Alp

Reputation: 6434

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 ?

Upvotes: 11

Ravindran
Ravindran

Reputation: 37

string id = Request.QueryString["RID"].ToString(); 
userid=Convert.ToInt32(id);

Upvotes: 1

Rebecca
Rebecca

Reputation: 14412

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.

Upvotes: 2

JacquesB
JacquesB

Reputation: 42669

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.

Upvotes: 0

Ole Lynge
Ole Lynge

Reputation: 4587

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

Upvotes: 0

Alex Reitbort
Alex Reitbort

Reputation: 13706

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

Upvotes: 3

Related Questions