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

  • SEARCH
  • Home
  • 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 9128017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:24:50+00:00 2026-06-17T07:24:50+00:00

I have very basic category model ID, RootCategoryID, Name and if I have category

  • 0

I have very basic category model ID, RootCategoryID, Name and if I have category that has many childrens it won’t delete so I need to do this recursively but when I do so I get error.

I know that there is a work-around if I add MultipleActiveResultSets=true in connection string but AFAIK this can be solved from within code and it is not a good idea to use this parameter. Is this true?

Error

There is already an open DataReader associated with this Command which
must be closed first.

Code

public ActionResult Delete(int id)
{
    this.DeleteRecursive(id);
    _db.SaveChanges();
    return RedirectToAction("index", "category");
}

private void DeleteRecursive(int id)
{
    // Selecting current category
    var currentCategory = _db.Categories.Where(x => x.ID == id).Single(); // this line
    var childrenCategories = _db.Categories.Where(x => x.RootCategory.ID == id);

    // Check if category has children
    if (childrenCategories.Count() > 0)
    {
        // Loop through children and apply same function recrusively
        foreach (var c in childrenCategories)
        {
            this.DeleteRecursive(c.ID);
        }
    }

    // Category has no children left, delete it
    _db.Categories.Remove(currentCategory);
}
  • 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-06-17T07:24:52+00:00Added an answer on June 17, 2026 at 7:24 am

    You’re leaving the DataReader open for the childrenCategories statement.

    Apart from the exception, it means you’re executing the query twice – once to get the count and then again to get the data.

    This should fix the problems:

    var childrenCategories = _db.Categories
      .Where(x => x.RootCategory.ID == id)
      .ToList()
    ;
    

    This executes the SQL statement and materializes all the records into a List.

    So, you have your data in memory, and the DataReader is complete.

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

Sidebar

Related Questions

I have a very basic mysql table called memberships, that tracks which people belong
I have a very basic API in my app which also has an index
I have some very basic semaphore code that works great on Linux, but cannot
I have a very basic doubt regarding the method that gets executed when app
I have a very basic data class that is subclassed from NSObject. I declare
I have a very basic jquery mobile modal dialog that INTERMITTENTLY reloads on clicking
I have a very basic table with few columns. It has 15 records in
I have a very basic json file that I am loading with jquery /
I have a very basic theme that I created and I'm running into a
I have a very basic container view that contains a sidebar and swaps out

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.