how to validate QueryString?

J

Jassim Rahma

I have an ID which I pass from the QueryString["id"]. The id is integer only
and I want before performing any action to validate if the Id is valid
integet.
 
G

Guest

I have an ID which I pass from the QueryString["id"]. The id is integer only
and I want before performing any action to validate if the Id is valid
integet.

for example

int id;
try {
id = Request.QueryString["id"];
} catch {
return; // id is wrong
}
 
A

Aidy

int id = 0;
if (int.TryParse(QueryString["id"], out id))
{
// id is ok
}
else
{
// it isn't
}
 
S

S. Justin Gengo

Alexey,

You should do some research on using Try/Catch to take care of programming
logic. Doing so should be avoided at all cost. Using Try/Catch is very
inneficient and can make applications perform very slowy.

Aidy's example utilizing TryParse is an incredibly more efficient way to
handle this.

Regards,

--
S. Justin Gengo, MCP
justin@aboutfortunate[-NoSpam-].com

Free code library at:
www.aboutfortunate.com




Anon User said:
I have an ID which I pass from the QueryString["id"]. The id is integer
only
and I want before performing any action to validate if the Id is valid
integet.

for example

int id;
try {
id = Request.QueryString["id"];
} catch {
return; // id is wrong
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

S. Justin Gengo said:
Alexey,

You should do some research on using Try/Catch to take care of
programming logic. Doing so should be avoided at all cost. Using
Try/Catch is very inneficient and can make applications perform very slowy.

Aidy's example utilizing TryParse is an incredibly more efficient way to
handle this.

In this case either way is valid. A non-integer value might very well be
considered to be an exceptional situation. In that case performance is
not an issue. It's true that exceptions should not be used in normal
program flow, but if the validation fails, the normal program flow is
out of the picture anyway.

-

The catch, however, should _not_ catch any exception, it should catch
only FormatException and OverflowException. Don't catch exceptions that
you don't know how to handle.

Also the code handling the exception should _not_ simply do a return, it
should actually handle the situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top