I wrote the following code to display data from three table by using union all
Two tables has values whereas 3rd table does not contain any value. Hence it is showing me following error
Exception Details: System.InvalidCastException: Cannot cast DBNull.Value to type 'System.Decimal'. Please use a nullable type.
Source Error:
decimal totalDis = sTable.AsEnumerable().Sum(row => row.Field<decimal>("Amount"));
private void BindGrid()
{
con = new SqlDbConnect();
con.SqlQuery(@"select 'Dispatch Local' as 'Head',Sum(TotalPrice) 'Amount' from tblDispatchLocal
union all
select 'Sales Trans:' as 'Head',Sum(P_Price) 'Amount' from tblSalesTran
union all
select 'Loan Recovery:' as 'Head',Sum(R_Amount) 'Amount' from tblLoanRefund;
;");//
adapt.SelectCommand = con.Cmd;
adapt.Fill(sTable);
if (sTable.Rows.Count > 0)
{
GridView1.DataSource = sTable;
GridView1.DataBind();
GridView1.FooterRow.Cells[0].Text = "Total:";
GridView1.FooterRow.Cells[0].Font.Bold = true;
GridView1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;
decimal totalDis = sTable.AsEnumerable().Sum(row => row.Field<decimal>("Amount"));
GridView1.FooterRow.Cells[1].Text = totalDis.ToString();
GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
GridView1.FooterRow.Cells[1].Font.Bold = true;
GridView1.FooterRow.Cells[1].BackColor = System.Drawing.Color.Yellow;
}
else
{
Response.Write("No Record Found");
return;
}
con.conClose();
}
how to get solution plz