Monday 10 June 2013

How to check string is number or not in C#

 Some time we have required to check the value of string is Number or not if it no then offcourse       our   task on these like sum, avg, max etc etc but somehow it contain string value is not Numeric then its panic . Then what is the solution
Simply check the no if it is numeric then perform the action other wise no action performed.

Below is the simple example .
there are a method in C# which check the datavalue. Method name is TryParse
it returns always boolean value. 

Solution :

string strValue = "ABC";
double Num;
bool isNum = double.TryParse(Str, out Num); // out parameter are used
if (isNum)
Response.Write(Num.ToString());  // you use here your label id or other thing
else
Response.Write("Invalid number");
  // you use here your label id or other thing