Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        decimal total = 0;
        decimal profit = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            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; // Here error occurs
        }
    }


Error message: Object Reference not set to an instance of an object.
Posted
Updated 20-Mar-14 0:06am
v2
Comments
Krunal Rohit 20-Mar-14 6:01am    
Code looks promising.
You sure there is a label with mentioned name ?

-KR
lukeer 20-Mar-14 6:10am    
What data type is there originally in e.Row.DataItem?

Your'e converting it to decimal, convert the result, which is a decimal to decimal again, and then to string for displaying it. Maybe there is a shorter, less error prone conversion path?

Mostly, This Error Occurred If You Can Not Find Control In Page Like "lbltotal".
Please Check This.
Hope This Will Help You.
 
Share this answer
 
Comments
CHill60 20-Mar-14 8:01am    
Agreed - if the error occurs where the OP's comment indicates then the earlier Label lblTotal=(Label)e.Row.FindControl("lbltotal"); failed to find the control "lbltotal" ... therefore variable lblTotal is null.
Note to OP - check your spelling and letter-case
Always check the value of a variable if it could be null!

C#
protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    decimal total = 0,
            profit = 0;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        object AchievedPer = DataBinder.Eval(e.Row.DataItem, "AchievedPer");

        if(AchievedPer != null)
        {
            object obj = e.Row.FindControl("lbltotal");

            if(obj != null)
            {
                Label lblTotal = (Label)obj;
                lblTotal.Text = AchievedPer.ToString();
            }
        }
    }
}
 
Share this answer
 
v2
Comments
norbitrial 20-Mar-14 7:45am    
What would you like to achieve with your code?
Gladiator3 21-Mar-14 6:24am    
If I get my expected result at lblTotal.Text, then I could assign my received value as a 'Total' in my footer template.
norbitrial 21-Mar-14 6:37am    
The exception occurs because the stotal variable is null, if you check the variables value you won't get the exception. You can see my code below.
onvert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));

You cannot convert a string type to decimal.
 
Share this answer
 
Comments
CHill60 20-Mar-14 7:59am    
But ToDecimal takes a String, and it looks like field "AchievedPer" holds a percentage, so what would be the problem with this line?
Abhinav S 20-Mar-14 8:03am    
Percentage % cannot be converted to an integer.
CHill60 20-Mar-14 8:17am    
I guess that's why the OP used ToDecimal :-)
Gladiator3 21-Mar-14 0:15am    
Hi,
It holds a decimal, only the naming looks like it is percentage...
Still,problem not solved...
Please check the code:
protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
{
decimal total = 0;
decimal profit = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{

Label lblTotal=(Label)e.Row.FindControl("lbltotal");
total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
string stotal = Convert.ToDecimal(total).ToString() ;<--decimal value is saved as string
lblTotal.Text = stotal; // Here error occurs<---then why I'm unable to put a string value to a label,which accepts string values..?
}
}
CHill60 21-Mar-14 5:18am    
See solution 1 and my comments to that solution. You do not have a label control called "lbltotal" ... perhaps it is called "lblTotal"?

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