1

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.

3
  • Could you provide the complete stack trace? Commented Jun 1, 2022 at 15:07
  • Something seems off - where is the DbContext in any of this code? Commented Jun 1, 2022 at 15:08
  • Try using Async Task & await in your controller methods Commented Jun 1, 2022 at 15:11

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

var records = await _db.FeedbackData; This line gives me an syntex error for missing a using directive or assembly reference
What does _db.FeedbackData should do? is it a table in your database? if you want to retrieve records for this table you should use _db.FeedbackData.ToListAsync()
yes it is. let me try this ToListAsync() method
Bingo!! Thanks a lot. you saved me a lot of vain efforts. it worked like I wanted it to. Thanks again 😊

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.