我在我的應用程序中遇到了一個奇怪的錯誤。它出現在級別或錯誤消息:Linq 2 Sql - 已打開datareader問題
- 已經有和打開的數據讀取器關聯。 然後來到
- 無效嘗試調用讀取器關閉時讀取。 然後來到
- 索引超出了數組的範圍。 然後來到
- 指定的轉換無效。
讓我首先解釋我的代碼在做什麼: 我正在使用我的Linq-Sql應用程序的存儲庫模式。從這個倉庫我調用這個方法
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
};
}
}
這種方法是從兩個表中,父母與子女,並返回結果作爲類的一個新的集合
public class ParentChild
{
public Category Parent { get; set; }
public IEnumerable<SubCategory> Childs { get; set; }
}
有時它工作正常,但是當採摘行流量增加和併發性,那麼在這種情況下,我開始得到這些錯誤。來到這個問題,從UI我消耗IEnumerable<ParentChild> GetAllCategoriesAndSubcategories()
顯示它在heirarchy。
在UI我使用這種方法來渲染文本:
/// <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.
}
我收到錯誤@foreach (ParentChild item in dt)
。請幫幫我。
建議要求: 我用這作爲我的回購模式實現:
internal sealed class LeadsRepository : IRepository<BuySell>
{
private static readonly BusinessBazaarDataContext Context;
static LeadsRepository()
{
Context = new BusinessBazaarDataContext();
}
}
我不認爲它不使用DataContext的一個好方法。請建議我...謝謝
我會通過構造函數注入將'DataContext'傳遞給'LeadsRepository'。 [我不特別喜歡靜態。](https://sites.google.com/site/steveyegge2/singleton-considered-stupid)你可以使用控制反轉(IoC)框架(例如Ninject,StructureMap,Windsor Castle )來處理終身關切。 – 2012-04-13 14:19:41