C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Get int Id from the Querystring(TryParse)


Posted Date: 18 Nov 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: JessicaMember Level: Gold    
Rating: 1 out of 5Points: 5



Hi All,

We can get int Id from the query string using tryparse method. I have noticed in many code that developer using the following method to get Id.


if(Request.QueryString!=null)
{
if(Request.QueryString.Count > 0)
{
if(Request.QueryString["Id"]!=null && Request.QueryString["Id"]!=string.Empty)
{
int Id = Convert.Toint32(Request.QueryString["Id"]);
}
}
}


But this is not a good way to do so. One can do this with the TryParse method of the int. Like shown as below.


int Id= 0;
if((int.TryParse(Request["Id"]),out Id)
{
//do some work
}
else
{
//do some other work
}


Benefit:
(1) We can avoid too many if..else condition and ultimately less LOC(Line of code)
(2) We can check the querystring null and blank condition simultaneously
(3) Main part is "out Id". If the query string is not null and have value then
it will be assign to the Id variable. So in the if condition we can use it directly.
(4) More Readable.

Limitations:
(1) Only applicable to the int parameter as a query string.

Regards.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
TryParse  .  Querystring  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Reference Parameters in C# with small example
Previous Resource: Event Handling in C#
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use