tags:

views:

320

answers:

5

I have a query string called propID and I wanna check if the passed value in it is a legal integer or not to avoid throwing an error that might reveal info about my database, how can I do it?

In other words, I want something like -but in vb.net- :

IF QueryString("propID").Content.Type = "int32" Then Proceed
+8  A: 

You could use TryParse:

Dim myInt As Integer
If Int32.TryParse(QueryString("propID").Content, myInt) Then Proceed
Chris Pebble
Will try parse work correctly with an empty string (e.g. fail) or will it give 0?
Stevo3000
Empty Strings will not evaluate to 0
TheTXI
Today I just discovered that although TryParse will return false for empty strings, it will also set myInt equal to 0. 'Tis very annoying when you want to use -1 as a default.
David
TryParse always sets myInt to 0 if its return value is false.
M. Jahedbozorgan
+3  A: 
Dim result as boolean
result = integer.tryparse(QueryString("propID"), myintegervariable)

boolean will return true if it parsed correctly (putting the value into your myintegervariable) and will return false if the parsing failed.

You can also write is as

if integer.tryparse(QueryString("propID"), myintegervariable) then
    //continue going along with myintegervariable
else
   //parsing didn't work
end if
TheTXI
As has been said in other answers, you can use Int32.tryparse just as well as any other data type that you may need. I use integer more because of habit.
TheTXI
A: 

You can just use Int32.TryParse.

BobbyShaftoe
A: 

You could try the 'is' keyword to check the type of on object.

If QueryString("propID").Content.Type Is Int32 Then Proceed

Otherwise Int32.TryParse would work as well.

Scott Lance
A: 

C# version:

int _PropID;
if (int.TryParse(QueryString["propID"], out _PropID))
{
    //Proceed with _PropID
}
M. Jahedbozorgan