Click here to Skip to main content
15,217,205 members
Rate this:
Please Sign up or sign in to 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..?
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));
Gladiator3 21-Mar-14 1:25am
I had tried that earlier...still the error persists..
Object reference not set to an instance of an object.

For your kind information "AchievedPer" value is of decimal datatype.

Any other solution?
Ankur\m/ 21-Mar-14 1:52am
The error means the value of AchievedPer is null. Attach a debugger and add a watch for DataBinder.Eval(e.Row.DataItem, AchievedPer). You will find that it is null.
Gladiator3 22-Mar-14 0:24am
could u share u mail id...?
I had placed a debugger and I did get my required solution,for variable 'total'.
Ankur\m/ 24-Mar-14 1:38am
May I know why do you need my email id for? You can post anything here.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You should use Decimal.TryParse Method[^]

Example added as requested:
using System;

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

        Decimal num;

        if (Decimal.TryParse(str, out num))
            Console.WriteLine("Unable to parse '{0}'.", str);
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 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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100