I have following code with six CA2000 warnings. and i get this warning when i do code analysis. please let me know how to overcome this warning and why i get this warning. please do help me how to clear this warning and thanks in advance.
if (e.Row.RowType == DataControlRowType.Footer)
{
decimal num3 = 0;
foreach (GridViewRow gridViewRow in this.gvTax.Rows)
{
Label label2 = gridViewRow.FindControl("lbltax") as Label;
num3 += Convert.ToDecimal(label2.Text);
}
int count = e.Row.Cells.Count;
for (int i = 0; i <= count - 1; i++)
{
e.Row.Cells[i].Visible = false;
}
TableHeaderCell tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = "Total Commission";
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(0, tableHeaderCell);
tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = Math.Round(num, 2).ToString();
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(1, tableHeaderCell);
tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = "Net Commission";
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(2, tableHeaderCell);
tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = Math.Round(num - num3, 2).ToString();
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(3, tableHeaderCell);
tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = "Total Deduction";
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(4, tableHeaderCell);
tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = Math.Round(num3, 2).ToString();
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(5, tableHeaderCell);
}
}
warnings are following.
01 ) Warning 1 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.
02) Warning 2 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.
03) Warning 4 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.
04) Warning 4 CA2000 : Microsoft.Reliability : In method 'Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.
05) Warning 5 CA2000 : Microsoft.Reliability : In method 'PayCommission.gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.
06) Warning 6 CA2000 : Microsoft.Reliability : In method 'PayCommission.gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)', object 'tableHeaderCell' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'tableHeaderCell' before all references to it are out of scope.
The warning is displayed because you are creating an instance of TableHeaderCell. TableHeaderCell implements IDisposable and you are not disposing it. In this case the warning is probably a red herring, and you can safely ignore it (right click warning -> suppress -> In Source) as the control should dispose of all these things when it is disposed.