Click here to Skip to main content
12,828,476 members (46,714 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 16 Oct 2012

TryParse Methods

, 16 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
TryParse Methods

TryParse methods which is provided by the .net framewok is one of the important method when you are converting string data into perticular primary datatype and wants to verify that it is convertable in given type or not.


boolean datatype.TryParse(stringvariable, out datatypevariable)

As in syntax TryPrase method of the datatype try to convert stringvalue in the given datatype if it succeeds than returns true and assign value to datatypevariable otherwise return false.

Where to Use :

- Avoid runtime error of converting explicite data type.

object s = "5b";
        int a = (int)s;
- To replace Parse Method code which is throws runtime exception.
string s = "5b";
        int a = int.Parse(s);

- When you want to validate data which you are reading from your textbox.


I am going to discuss about the case where I found it useful and how it's helpful to you in your project.
Recently I was working on project which is reading data from the excel file which contains employee details and converting that excel in the DataTable format.

Find how to read excel in datatable : Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function

Now the requirement to displaying the employee by employeeid and those employeeids are not valid I have to display them in last.
So the solution to this is make use of Linq with the int.Typarse Method.

int myInt;  
    var Employee = (from row in dtEmployee.AsEnumerable()
         orderby  (int.TryParse(row ["Employeeid"]).ToString(), out myInt) ?  myInt: 0 ) 
               select row ).ToList<datarow>();</datarow>

So the above method return the employeeid in asending order and display the row which are not integer at last.
As you can see TryPrase method of int in Linq query return employeeid if its integer and 0 if the employeeid is not integer.

MSDN Referance

Note : share the information where else it is useful.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 16 Oct 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid