i amgetting error.
Cannot cast DBNull.Value to type 'System.Decimal'. Please use a nullable type.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Cannot cast DBNull.Value to type 'System.Decimal'. Please use a nullable type.
Source Error:
Line 220: this.GridView1.DataBind();
Line 221: //decimal total = Convert.ToDecimal(dt.Rows[i][2].ToString());
Line 222: decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("Reportable"));
Line 223: GridView1.FooterRow.Cells[1].Text = "Total";
Line 224: GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
|
private void BindGrid(string Month, string year)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("ld_lg_count3", con))
{
if (!string.IsNullOrEmpty(Month) && !string.IsNullOrEmpty(year))
{
cmd.Parameters.AddWithValue("@Month", Month);
cmd.Parameters.AddWithValue("@year", year);
}
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
//decimal total = Convert.ToDecimal(dt.Rows[i][2].ToString());
decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("Reportable"));
GridView1.FooterRow.Cells[1].Text = "Total";
GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
GridView1.FooterRow.Cells[3].Text = total.ToString();
}
}
}
}