Witam,
Mam problem z DropDownList, mianowicie podczas uruchamiania lokalnie aplikacji wywala błąd:
Error: There is already an open DataReader associated with this Command which must be closed first.
Wraz z informacją źródła błędu:
Line 51: @Html.DropDownList("ScreeningId", null, htmlAttributes: new { @class = "form-control" })
Umieszczam dodatkowo pliki View oraz Controller.
public class ReservationsController : Controller
{
private AppDbContext db = new AppDbContext();
// GET: Reservations
public ActionResult Index()
{
return View(db.Reservations.ToList());
}
// GET: Reservations/Create
public ActionResult Create()
{
ViewBag.ScreeningId = new SelectList(db.Screenings, "Id", "Description");
HashSet<int> seats = new HashSet<int>(db.Reservations.Select(x => x.SeatNumber));
ViewBag.Seats = seats;
return View();
}
// POST: Reservations/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,FirstName,SecondName,Phone,SeatNumber,ScreeningId")] Reservation reservation)
{
if (ModelState.IsValid)
{
// sprawdzamy czy miejsce bylo juz zajete
if (db.Reservations.Select(x => x.SeatNumber).Contains(reservation.SeatNumber))
{
ViewBag.ScreeningId = new SelectList(db.Screenings, "Id", "Description");
return View(reservation);
}
else
{
db.Reservations.Add(reservation);
db.SaveChanges();
}
if (Session["Login"] != null)
{
return RedirectToAction("Index");
}
return RedirectToAction("Success");
}
return View(reservation);
}
<div class="form-group">
@Html.LabelFor(model => model.Screening, "Seans", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ScreeningId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Screening, "", new { @class = "text-danger" })
</div>
</div>
Aplikacja jest umieszczona na hostingu darmowym. Lokalnie gdy uruchamiam, błędu w tym miejscu nie wywala.