Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Label lblTotal=(Label)e.Row.FindControl("lbltotal");
total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
string stotal = Convert.ToDecimal(total).ToString() ;
lblTotal.Text =stotal.ToString();//<---Error occurs in this statement


How to type cast in the best way?
Posted
Comments
King Fisher 20-Mar-14 7:22am    
whats your error?

try this.. :)

C#
Label lblTotal = (Label)e.Row.FindControl("lbltotal");
total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
decimal stotal = Convert.ToDecimal(total);
lblTotal.Text = Convert.ToString(stotal);
 
Share this answer
 
Simple: it can't find a control called "lbltotal" in the row, so it returns a null value.
When you try to set the Text property of a null value it throws a "Object reference is required" exception.

So look at your row, and work out why it doesn't have "lbltotal". There are so many possible reasons why, but we can't see your code, so we can't tell you.
Start by using the debugger and looking at e.Row in detail immediately after the line:
C#
Label lblTotal=(Label)e.Row.FindControl("lbltotal");
You need to know what it does contain, so you can look at what you thought it contained!
 
Share this answer
 

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