Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, AchievedPer));
string stotal = Convert.ToDecimal(total).ToString() ;

lblTotal.Text = stotal;//string Label.text=(local variable)string//Conversion failed..



Please let me know,why I'm unable to get the result at lblTotal.Text..?
Posted
Comments
ravikhoda 21-Mar-14 0:44am    
if "Conversion failed.." is your error try and check what value you are getting from DataBinder.Eval(e.Row.DataItem, AchievedPer). it looks like this value is not convertable from string to decimal in the first line.
Gladiator3 21-Mar-14 1:26am    
Seems true, but, I need to get a solution..
syed shanu 21-Mar-14 0:52am    
Check for empty and null value here total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, AchievedPer));
Gladiator3 21-Mar-14 1:31am    
Yes, But how can u check for decimal value=null, it would obviously show an error, 'The result of the expression is always 'true' since a value of 'type' decimal is never equal to null of type 'decimal'? '
Ankur\m/ 21-Mar-14 1:07am    
Why are you doing so many conversions? just below line of code will do the work;
lblTotal.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, AchievedPer));

try decimal.parseexact or tryparse method it may help or from take care from your database query and handle it by usign ISnull('ColumnName','0.0') so if your data it null it will return 0.0 and your conversion to decimal will not affect.
 
Share this answer
 
You should use Decimal.TryParse Method[^]

++++++++++++++++++++++++++++++++
Example added as requested:
VB
using System;

public class Program
{
    public static void Main()
    {
        string str = "123.45";

        Decimal num;

        if (Decimal.TryParse(str, out num))
            Console.WriteLine(num);
        else
            Console.WriteLine("Unable to parse '{0}'.", str);
    }
}
 
Share this answer
 
v2
Comments
Gladiator3 21-Mar-14 1:41am    
Hi Peter,
Could you please, explain explicitly in web application, as I see your example in a console application.
Peter Leow 21-Mar-14 2:05am    
Example added.
Gladiator3 21-Mar-14 2:18am    
Hi Peter,
Could you please, explain explicitly in web application.
Peter Leow 21-Mar-14 2:24am    
What web application? This is generic. You can adapt this in the code behind of asp.net or in winform. I think I have already provided you the solution.
lukeer 21-Mar-14 2:32am    
The console part is just for showing the result to the user. You have to adapt this to your situation. It's the parsing that should be shown here.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900