views:

2201

answers:

5

How do i convert a Request.Quesry 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. Im using the string value as an input to a stored procedure which takes in only integer types for that field.. Pls help. Its urgent. Thanks in advance.. 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;
+2  A: 

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

Alex Reitbort
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..
look in debugger what you have inside rid variable.
Alex Reitbort
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
what is the VALUE of variable rid? What is displayed on the label?
Alex Reitbort
the label displays integer values.. RID is an integer
CAN YOU POST THE VALUE HERE PLEASE?!?!?!?
Alex Reitbort
+4  A: 
int foo;
int.TryParse(Request.Querystring["foo"], out foo);

or just like you say, int.Parse should convert it

Could you post some code here ?

Barbaros Alp
string rid=Request.QueryString["RID"]; lblRID.Text = rid; int id= Int32.Parse(rid);I've tried that andstring rid=Request.QueryString["RID"]; lblRID.Text = rid; int id= Convert.ToInt32(lblRID.Text) or int id= Convert.ToInt32(rid)
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
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
A: 
Ole Lynge
A: 

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
+2  A: 

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"].ToString();

        //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 M