Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6183379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:21:18+00:00 2026-05-24T01:21:18+00:00

I need some help with this error The ObjectContext instance has been disposed and

  • 0

I need some help with this error “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.”

It’s a asp.net mvc3, EF4, and ms sql.
Here is the razor with two dropdowns:

<div class="editRow">
@Html.DropDownListFor(m=>m.IndustryId, (SelectList)ViewBag.Industry, @Empower.Resource.General.ddlDefaultVal, new { @class = "ddl400" })
@Html.ValidationMessageFor(m => m.IndustryId)
</div>
<div class="editRow">
@Html.DropDownListFor(m=>m.ProvinceId, (SelectList)ViewBag.Province, @Empower.Resource.General.ddlDefaultVal, new {@class = "ddl400"})
 @Html.ValidationMessageFor(m => m.ProvinceId)   
</div>

Controller:

 IndustryService indService = new IndustryService();
ViewBag.Industry = new SelectList(indService.GetAllIndustry(), "IndustryId", "IndustryName");
ProvinceService proService = new ProvinceService();
ViewBag.Province = new SelectList(proService.GetAllProvince(), "ProvinceId", "ProvinceName");
 return View();

ProvinceService:

public IEnumerable<Province> GetAllProvince()
        {
            using (var context = DBContext.ObjectContext)
            {
                var pros = context.Provinces;
                return pros;
            }
        }

IndustryService is identical as above…

public class DBContext
    {
        private static EmpowerDBEntities _empowerContext;
        public static EmpowerDBEntities ObjectContext
        {
            get
            {
                if (_empowerContext == null)
                    _empowerContext = new EmpowerDBEntities();
                return _empowerContext;
            }
        }
    }

I know the problem occurs in second dropdown when it tries to retrive data while the connection is desposed by previous query. Please help me with this, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:21:19+00:00Added an answer on May 24, 2026 at 1:21 am

    The fix is simple – convert to a .ToList() or First() before using. LINQ has deferred execution and tries to run this command after the context is disposed (when your object results are referenced) – not when you actually make the call.. You need to force it to run now while the context is in scope.

    using (var context = DBContext.ObjectContext)
    {
         var pros = context.Provinces;
         return pros.ToList();
    }
    

    Also – your code above is checking for null in the get accessor. However this object won’t be null – it will be disposed, so you cannot do your check this way, you need to check if its null and not disposed.

    
    public class DBContext
        {
            private static EmpowerDBEntities _empowerContext;
            public static EmpowerDBEntities ObjectContext
            {
                get
                {
                    if (_empowerContext == null || _empowerContext.IsDisposed())
                        _empowerContext = new EmpowerDBEntities();
                    return _empowerContext;
                }
            }
        }
    
    

    something like that anyways 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help with this error. Fresh wordpress 2.9 install... Fatal error: Cannot instantiate
I need some help with my program. I get this error when I run
Guys i need some help here , im getting this error here at the
I really need some help with this as I have been trying to fix
I need some help with my xpath query. I can get this code to
guys i really REALLY need some help on this one. 2 days i've been
I have 2 scenarios that I need some help with re validation in my
I need some help making this program for class. We are working with g++
I need some help from the shell-script gurus out there. I have a .txt
I need some help regarding algorithm for randomness. So Problem is. There are 50

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.