Q:

Error when summing values in an asp.net gridview

I have the following code to sum values in a gridview, but I receive the error "Input string was not in a correct format" at the line 
cell1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CTDActual"));

This is the complete snippet:
decimal cell1 = 0;

protected void linqGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        cell1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CTDActual"));
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[3].Text = cell1.ToString("C");
    }
}

I tried outputting the DataItem value to the console window, but nothing appears. How can I fix this problem?

A:

Try with this:
cell1 += Convert.ToDecimal((DataBinder.Eval(e.Row.DataItem, "CTDActual").ToString()==""?"0":DataBinder.Eval(e.Row.DataItem, "CTDActual")));

The reason is blowing up is likely because you are getting a DbNull.Value from the database, which is causing the Convert.ToDecimal to blow up since it doesn't know how to convert "" to a number.
UPDATE:
This will definitely work:
decimal temp = 0M;
decimal.TryParse(DataBinder.Eval(e.Row.DataItem, "CTDActual").ToString(), out temp);
cell1+=temp;

