'Used IDisposible for Object class but not returing the result to Ajax success in C# MVC [duplicate]

In our application for the last few weeks we are getting memory exceptions.

W​herever we used DataSet & DataTable​, we used the Using Statement or Dispose method in Finally black.

For our Class objects not able to dispose of the stored values. In our class object we are storing large JSON strings from the database.

For this issue Microsoft recommended the IDisposable concept. We implement this concept in our class object.

So added the IDisposable in the class object and we are trying to call the Dispose method in Finally block. which is after returning the values to Jquery Ajax success method.

But In Ajax success method we didn't get the actual values from the controller. it is disposing the values before sending to the Jquery Ajax call.

Please find and below code and give your input.

ET_Reports dynamicGrid = new ET_Reports();
     try
        {       
            dynamicGrid = ReportAPI.GetDynamicReportGrid();
            var jsonResult = Json(dynamicGrid, JsonRequestBehavior.AllowGet);
             jsonResult.MaxJsonLength = int.MaxValue;
             return jsonResult;                   
            
        }
        catch (Exception ex)
        {
            string strDisplayMsg = Log_Exception(ex);
            return null;
        }
        finally
        {
            dynamicGrid.Dispose();
        }


Solution 1:[1]

I think, you need to dispose dynamicGrid before return statement or replace return after finally statement. Any way you need to dispose before return.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Areg Gevorgyan