Lỗi: There is already an open DataReader associated with this Command which must be closed first

Thảo luận trong 'Lập Trình Website MVC5 & MVC6' bắt đầu bởi seolagi, 30/10/20.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,027
    Đã được thích:
    80
    Điểm thành tích:
    48
    Cho mình hỏi web asp.net MVC 5 của mình thực hiện lệnh linq như dưới thì bị lỗi: DataReader associatLỗi: There is already an opeed with this Command which must be closed first
    Code của mình như sau: nó báo lỗi như hình dưới, cảm ơn mọi người.
    Mã:
     
    foreach (var cate in dbc.Categories.Where(s => s.Type == 1 || s.Type == 3).OrderByDescending(p => p.Seq))
    {
        cateurl = "";
        cateurl = HttpWeb + "/" + cate.URLName;
        Urls = "";
        Urls = cateurl;
        if (Urls != "")
        {
            HtmlAll += "<li><ul>";
            //9 danh sách tin tức
            foreach (var news in dbc.NewsCts.Where(p => p.Available && p.TypeData == "news").OrderByDescending(p => p.EditeTime))
            {
                Urls = "";
                Urls = Url.Action("Detail", "News", new { seo = XString.Filter(news.URLName) }, Request.Url.Scheme);
                HtmlAll += Urls != "" ? "<li>" + Urls + "</li>" : "";
            }
            HtmlAll += "</ul></li>";
        }
    }
    
    loi-there-is-already-an-open-datareader-associated-with-this-command-which-must-be-closed-first.png
     
    Cảm ơn đã xem bài:

    Lỗi: There is already an open DataReader associated with this Command which must be closed first

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,883
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Bạn thêm .ToList() vào nhé.

    Sửa code thành như sau:
    Mã:
    foreach (var cate in dbc.Categories.Where(s => s.Type == 1 || s.Type == 3).OrderByDescending(p => p.Seq).ToList())
    {
        cateurl = "";
        cateurl = HttpWeb + "/" + cate.URLName;
        Urls = "";
        Urls = cateurl;
        if (Urls != "")
        {
            HtmlAll += "<li><ul>";
            //9 danh sách tin tức
            foreach (var news in dbc.NewsCts.Where(p => p.Available && p.TypeData == "news").OrderByDescending(p => p.EditeTime).ToList())
            {
                Urls = "";
                Urls = Url.Action("Detail", "News", new { seo = XString.Filter(news.URLName) }, Request.Url.Scheme);
                HtmlAll += Urls != "" ? "<li>" + Urls + "</li>" : "";
            }
            HtmlAll += "</ul></li>";
        }
    }
     


Chủ để tương tự : Lỗi already
Diễn đàn Tiêu đề Date
Lập Trình Website MVC5 & MVC6 Lỗi: You are debugging a Release build of .dll. Using just My Code in Visual Studio 2019 8/6/23
Lập Trình Website MVC5 & MVC6 Lỗi: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities. 27/3/20
Lập Trình Website MVC5 & MVC6 Lấy Entity Value old cũ sau đó Update không bị lỗi Asp.net MVC 27/3/20
Lập Trình Website MVC5 & MVC6 Lỗi Maximum request length exceeded khi upload file mvc asp.net c# 5/8/19
Lập Trình Website MVC5 & MVC6 Lỗi Convert bool to bool? in @Html.CheckBoxFor() trong ASP.NET MVC 4/1/19