I am busted with a weird bug in my app. It comes in levels or error messages:
- There is already and open datareader associated. Then comes
- Invalid attempt to call Read when reader is closed. Then comes
- Index was outside the bounds of the array. Then comes
- 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