Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi can any one telmme what type of error is this.?

here am getting error
 protected void grdFaculty_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
//for sum m getting error
            sum = sum + Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NoOfDuties"));//in this line m getting error
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label obj = (Label)e.Row.FindControl("Sum");
            obj.Text = Convert.ToString(sum);
        }
    }

like
Object cannot be cast from DBNull to other types.
can any one suggest me?
Posted
Updated 14-Mar-12 7:07am
v2

1 solution

I am assuming your syntax is correct. The line below returns DBNull which you are trying to cast to Int32.

C#
DataBinder.Eval(e.Row.DataItem, "NoOfDuties")


Instead:

C#
if (! DBNull.Value.Equals(DataBinder.Eval(e.Row.DataItem, "NoOfDuties"))
    sum = sum + Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NoOfDuties"));


More information is available here[^]

Here, I have assumed that DBNull means 0 for this field.
 
Share this answer
 
v2
Comments
ythisbug 14-Mar-12 13:20pm    
thanks shreekar but m getting error near DBNull is a type but used like a variable
shreekar 14-Mar-12 13:23pm    
I have edited the answer, I realised just after posting it.
ythisbug 14-Mar-12 13:31pm    
thanks brother

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