Hiindradeo,
You need to check with string.IsNullOrEmpty.
If its null then set the value to 0. So that you can avoid the error.
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="auto-style11" Height="138px" Width="99.7%" ShowFooter="true">
<Columns>
<asp:BoundField DataField="DEPARTMENT" HeaderText="DEPARTMENT" />
<asp:TemplateField HeaderText="Deviation">
<ItemTemplate>
<asp:LinkButton ID="Deviation" Text='<%# Eval("Deviation") %>' runat="server" CommandName="Deviation" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Near miss">
<ItemTemplate>
<asp:LinkButton ID="Near_miss" Text='<%# Eval("Near miss") %>' runat="server" CommandName="Near_miss" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Aid">
<ItemTemplate>
<asp:LinkButton ID="First_Aid" Text='<%# Eval("First Aid") %>' runat="server" CommandName="First_Aid" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fire">
<ItemTemplate>
<asp:LinkButton ID="Fire" Text='<%# Eval("Fire") %>' runat="server" CommandName="Fire" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Non-Reportable">
<ItemTemplate>
<asp:LinkButton ID="NonReportable" Text='<%# Eval("Non-Reportable") %>' runat="server" CommandName="NonReportable" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reportable">
<ItemTemplate>
<asp:LinkButton ID="Reportable" Text='<%# Eval("Reportable") %>' runat="server" CommandName="Reportable" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fatal">
<ItemTemplate>
<asp:LinkButton ID="Fatal" Text='<%# Eval("Fatal") %>' runat="server" CommandName="Fatal" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Linq;
VB.Net
Imports System.Data
Imports System.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("DEPARTMENT"),
new DataColumn("Deviation"),
new DataColumn("Near miss"),
new DataColumn("First Aid"),
new DataColumn("Fire"),
new DataColumn("Non-Reportable"),
new DataColumn("Reportable"),
new DataColumn("Fatal")
});
dt.Rows.Add("EMG", "", "", "1", "", "", "", "");
dt.Rows.Add("EEMG", "", "", "", "1", "", "", "");
dt.Rows.Add("FES", "", "", null, "", "", "", "1");
dt.Rows.Add("IT", "", "", "", "", "1", null, "");
dt.Rows.Add("Mechanical Erection", "", "", "1", "", "", "", "");
GridView1.DataSource = dt;
GridView1.DataBind();
decimal deviation, near_miss, first_Aid, fire, non_Reportable, reportable, fatal;
deviation = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Deviation")) ? row.Field<string>("Deviation") : "0"));
near_miss = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Near miss")) ? row.Field<string>("Near miss") : "0"));
first_Aid = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("First Aid")) ? row.Field<string>("First Aid") : "0"));
fire = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Fire")) ? row.Field<string>("Fire") : "0"));
non_Reportable = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Non-Reportable")) ? row.Field<string>("Non-Reportable") : "0"));
reportable = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Reportable")) ? row.Field<string>("Reportable") : "0"));
fatal = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Fatal")) ? row.Field<string>("Fatal") : "0"));
GridView1.FooterRow.Cells[0].Text = "Total";
GridView1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;
GridView1.FooterRow.Cells[1].Text = deviation.ToString("N0");
GridView1.FooterRow.Cells[2].Text = near_miss.ToString("N0");
GridView1.FooterRow.Cells[3].Text = first_Aid.ToString("N0");
GridView1.FooterRow.Cells[4].Text = fire.ToString("N0");
GridView1.FooterRow.Cells[5].Text = non_Reportable.ToString("N0");
GridView1.FooterRow.Cells[6].Text = reportable.ToString("N0");
GridView1.FooterRow.Cells[7].Text = fatal.ToString("N0");
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("DEPARTMENT"),
New DataColumn("Deviation"),
New DataColumn("Near miss"),
New DataColumn("First Aid"),
New DataColumn("Fire"),
New DataColumn("Non-Reportable"),
New DataColumn("Reportable"),
New DataColumn("Fatal")})
dt.Rows.Add("EMG", "", "", "1", "", "", "", "")
dt.Rows.Add("EEMG", "", "", "", "1", "", "", "")
dt.Rows.Add("FES", "", "", Nothing, "", "", "", "1")
dt.Rows.Add("IT", "", "", "", "", "1", Nothing, "")
dt.Rows.Add("Mechanical Erection", "", "", "1", "", "", "", "")
GridView1.DataSource = dt
GridView1.DataBind()
Dim deviation, near_miss, first_Aid, fire, non_Reportable, reportable, fatal As Decimal
deviation = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(If(Not String.IsNullOrEmpty(row.Field(Of String)("Deviation")), row.Field(Of String)("Deviation"), "0")))
near_miss = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(If(Not String.IsNullOrEmpty(row.Field(Of String)("Near miss")), row.Field(Of String)("Near miss"), "0")))
first_Aid = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(If(Not String.IsNullOrEmpty(row.Field(Of String)("First Aid")), row.Field(Of String)("First Aid"), "0")))
fire = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(If(Not String.IsNullOrEmpty(row.Field(Of String)("Fire")), row.Field(Of String)("Fire"), "0")))
non_Reportable = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(If(Not String.IsNullOrEmpty(row.Field(Of String)("Non-Reportable")), row.Field(Of String)("Non-Reportable"), "0")))
reportable = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(If(Not String.IsNullOrEmpty(row.Field(Of String)("Reportable")), row.Field(Of String)("Reportable"), "0")))
fatal = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(If(Not String.IsNullOrEmpty(row.Field(Of String)("Fatal")), row.Field(Of String)("Fatal"), "0")))
GridView1.FooterRow.Cells(0).Text = "Total"
GridView1.FooterRow.Cells(0).HorizontalAlign = HorizontalAlign.Right
GridView1.FooterRow.Cells(1).Text = deviation.ToString("N0")
GridView1.FooterRow.Cells(2).Text = near_miss.ToString("N0")
GridView1.FooterRow.Cells(3).Text = first_Aid.ToString("N0")
GridView1.FooterRow.Cells(4).Text = fire.ToString("N0")
GridView1.FooterRow.Cells(5).Text = non_Reportable.ToString("N0")
GridView1.FooterRow.Cells(6).Text = reportable.ToString("N0")
GridView1.FooterRow.Cells(7).Text = fatal.ToString("N0")
End If
End Sub
Screenshot