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 7749931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:09:35+00:00 2026-06-01T11:09:35+00:00

I am busted with a weird bug in my app. It comes in levels

  • 0

I am busted with a weird bug in my app. It comes in levels or error messages:

  1. There is already and open datareader associated.
    Then comes
  2. Invalid attempt to call Read when reader is closed.
    Then comes
  3. Index was outside the bounds of the array.
    Then comes
  4. Specified cast is not valid.

Let me explain first what’s my code is doing:
I am using Repository Pattern for my Linq-Sql app. From that repository I calling this method

 internal static IEnumerable<ParentChild> GetAllCategoriesAndSubcategories()
        {
            lock (Context) // lock is implemented just before asking question, to check whether it can solve the issue or not...
            {
                return from p in Context.Categories
                       let relatedchilds = (from c in Context.SubCategories
                                            where c.CategoryId == p.Id
                                            select c).Take(5)
                       select new ParentChild
                       {
                           Parent = p,
                           Childs = relatedchilds
                       };
            }

        }

This method is picking rows from two tables, Parent and Child and return the result as a new collection of class

public class ParentChild
    {
        public Category Parent { get; set; }
        public IEnumerable<SubCategory> Childs { get; set; }
    }

Sometimes it works fine but when the traffic increases and concurrency then in that case i start getting these errors. Coming to the issue, From UI i am consuming IEnumerable<ParentChild> GetAllCategoriesAndSubcategories() to display it in heirarchy.

At UI i am using this method to render the text:

 /// <summary>
        /// Write categories Jquery html to the Category usercontrol
        /// </summary>
        private void WriteCategories()
        {

            // retrieves all categories and its subcategories as a generic list of ParentChild
            var dt = CategoryRepository.GetAllCategoriesAndSubcategories();

            //Conversion of dynamic jquery html string starts here
            var sb = new StringBuilder();
            sb.AppendLine(" <div class='widget_box' id='category'>");
            sb.AppendLine("     <div class='wintitle'>");
            sb.AppendLine("         <div class='inner_wintitle'>Categories</div>");
            sb.AppendLine("     </div>");
            sb.AppendLine("     <div class='winbody'>");
            sb.AppendLine("         <ul class='categories'>");
            var i = 1;
            foreach (ParentChild item in dt) //<--* BUGGY PART*
            {
                sb.AppendLine(
                    string.Format("<li class='catetitle' id='catetitle{0}'><a href='subcategory.aspx?cid={1}&cname={2}'>{2}</a></li>", i,
                                  item.Parent.Id, item.Parent.Name));
                sb.AppendLine(
                    string.Format("<li style='display:none;' class='category_sub' id='subcategory{0}' ><div><ul>", i));
                foreach (var subCategory in item.Childs)
                {
                    sb.AppendLine(string.Format("<li><a href='subcategory.aspx?cid={0}&cname={1}&scid={2}&scname={3}'>{3}</a></li>", item.Parent.Id,
                                                item.Parent.Name, subCategory.Id, subCategory.Name));
                }
                sb.AppendLine(
                    string.Format(
                        "<li class='catetitle' id='catetitle{0}'><a href='subcategory.aspx?cid={1}&cname={2}'>View all categories</a></li>",
                        i, item.Parent.Id, item.Parent.Name));
                sb.AppendLine("</ul></div></li>");
                i++;
            }
            sb.AppendLine("</div></ul></div>");
            //Conversion of dynamic jquery html string ends here

            // javascript function to display the subcategories when mouse is hovered to the category
            sb.AppendLine("<script type='text/javascript'>init_categories();</script>");
            ucCategories1.CategoryHtml = sb.ToString(); // Generated text is finally set to the usercontrols property.
        }

I am getting the bug @ foreach (ParentChild item in dt). Please help me.

Suggestion Required:
I am using this as my repo pattern implementation:

 internal sealed class LeadsRepository : IRepository<BuySell>
    {
        private static readonly BusinessBazaarDataContext Context;

        static LeadsRepository()
        {
            Context = new BusinessBazaarDataContext();
        }
}

I don’t thinks its a good way to use Datacontext. Please suggest me… 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-06-01T11:09:36+00:00Added an answer on June 1, 2026 at 11:09 am

    Switch to List<ParentChild> and return a fully populated List from your GetAllCategoriesAndSubcategories. Your lock would then be doing what you intende (so I assume).

    Specifically, you need to do:

    internal static IList<ParentChild> GetAllCategoriesAndSubcategories()
        {
            lock (Context) // lock is implemented just before asking question, to check whether it can solve the issue or not...
            {
                return (from p in Context.Categories
                       let relatedchilds = (from c in Context.SubCategories
                                            where c.CategoryId == p.Id
                                            select c).Take(5)
                       select new ParentChild
                       {
                           Parent = p,
                           Childs = relatedchilds
                       }).ToList();
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey. the busted website is: www.mgxvideo.com/mgxcopy-alpha-3 , and the specific error that I'm getting
I'd like to replace an app's current (badly busted and crufty) cURL-based (cURL command-line
Oddest error today: 2011-12-17 08:52:50.565 ShowLink[625:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
Someone else has already asked a somewhat similar question: Validate an Xml file against
There have been a lot of discussion around this and everyone tend to agree
I recently installed the Rails3.1-Devise-Rspec-Cucumber Starter App with the Gemfile listed below. This created
There's not much more to my question than that. gem install mysql doesn't work
I have a data gathering app (terrestrial magnetic field strength) that has four phases;
There are a lot of captchas plugins in Rails and also many types of
I'm writing a windows phone 7 app. I have fatal exception handling code where

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.