tags:

views:

16

answers:

1

How to fix CA2000 code analysis error in following function on statement DataTable dtSummary = new DataTable("Summary");? CA2000 going off If I use using block on dtSummary and I can't use using block in below function becuase some other function is going to use return datatable from GetSummaryReportTable function.

private DataTable GetSummaryReportTable(IImportResult result) {

DataTable dtSummary = new DataTable("Summary");
dtSummary.Columns.Add(STATUS_STRING_COL_NAME, typeof(string));
dtSummary.Columns.Add(STATUS_COL_NAME, typeof(int));
DataRow dataRow;

foreach (ReportErrorLevel error in distinctErrors)
{
    dataRow = dtSummary.NewRow();
    dataRow[STATUS_STRING_COL_NAME] = error.ToString();
    dataRow[STATUS_COL_NAME] = Convert.ToInt16(error);
    dtSummary.Rows.Add(dataRow);
}           
return dtSummary; 

}

+1  A: 

This is topic has been discussed thoroughly here: http://stackoverflow.com/questions/913228/should-i-dispose-dataset-and-datatable.

In short, if you really want to follow the style cop rules then you can't use DataTable as a return type. You must use some other object to return your data and wrap the DataTable object in a using statement.

Nathan Totten