Azhar Qureshi
Azhar Qureshi

Reputation: 57

ASP.NET Core MVC - System.ObjectDisposedException

This is the description of the error :

System.ObjectDisposedException: 'Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'AppDbContext'.'

I am trying to set database records into two objects which I have created in my controller file and use it for excel report generation. But when I click the generate report button, it gives this error.

Here is the code for the controller methods:

        private readonly AppDbContext _db;
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private static IEnumerable<Feedback> sheetfeedbacks;

 public AdminController(AppDbContext db, UserManager<ApplicationUser> userManager, IWebHostEnvironment webHostEnvironment)
        {
           _db = db;
           _userManager = userManager;
            _webHostEnvironment = webHostEnvironment;
        }


public IActionResult FeedbackMgmt()
{
    ParentForApply ParentVM = new ParentForApply();
    var records = _db.FeedbackData;
    ParentVM.FeedbackList = records;           
    sheetfeedbacks = records;
    return View(ParentVM);
}

public IActionResult ReportFeedback()
{           
    using (var workbook = new XLWorkbook())
    {
        var worksheet = workbook.Worksheets.Add("Feedback Data");
        var CurrentRow = 1;
        #region Header
        worksheet.Cell(CurrentRow, 1).Value = "Sr. No";
        worksheet.Cell(CurrentRow, 2).Value = "Full Name";
        worksheet.Cell(CurrentRow, 3).Value = "Suggestions";
        worksheet.Cell(CurrentRow, 4).Value = "Contact No";
        worksheet.Cell(CurrentRow, 5).Value = "Email Id";
        worksheet.Cell(CurrentRow, 6).Value = "City";
        worksheet.Cell(CurrentRow, 7).Value = "State";
        

        #endregion

        #region  Body
        foreach (var fback in sheetfeedbacks)
        {
            CurrentRow++;
            worksheet.Cell(CurrentRow, 1).Value = CurrentRow - 1;
            worksheet.Cell(CurrentRow, 2).Value = fback.FullName;
            worksheet.Cell(CurrentRow, 3).Value = fback.Suggestions;
            worksheet.Cell(CurrentRow, 4).Value = fback.ContactNo;
            worksheet.Cell(CurrentRow, 5).Value = fback.EmailId;
            worksheet.Cell(CurrentRow, 6).Value = fback.City;
            worksheet.Cell(CurrentRow, 7).Value = fback.State;        
            
        }
        

        #endregion

        using (var stream = new MemoryStream())
        {
            workbook.SaveAs(stream);
            var content = stream.ToArray();

            return File(
                content,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "Feedback Data.xlsx"
                );
        }
    }
}

Above mentioned error pops up at ReportFeedback() method.

Upvotes: 1

Views: 1285

Answers (1)

Merna Mustafa
Merna Mustafa

Reputation: 1373

The only place you are using AppDbContext in your controller is at this line

var records = _db.FeedbackData;

And as long as you want to access the database then you need to use Task & Await , that I suspect is the main cause for the error

public async Task<IActionResult> FeedbackMgmt()
{
    ParentForApply ParentVM = new ParentForApply();
    var records = await _db.FeedbackData.ToListAsync();
    ParentVM.FeedbackList = records;           
    sheetfeedbacks = records;
    return View(ParentVM);
}

Upvotes: 2

Related Questions