【发布时间】:2018-03-30 05:44:05
【问题描述】:
大家好,有人可以在这里给我帮助吗?我是新手,我不知道该怎么办!错误指向它所说的 SqlDataReader rdr = cmd.ExecuteReader(),
System.Data.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理
我的代码在下面。提前致谢。
EmpListModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
namespace AIStestv01.Models
{
public class EmpListModel
{
public string BioID { get; set; }
public string Fullname { get; set; }
public string Dthired { get; set; }
public string DeptID { get; set; }
public string Active { get; set; }
public string NTLogin { get; set; }
}
}
EmpController
using AIStestv01.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
namespace AIStestv01.Controllers.AdminControllers
{
public class EmpController : Controller
{
// GET: Emp
public ActionResult Index()
{
var con = ConfigurationManager.ConnectionStrings["WOPcon"].ConnectionString;
using (SqlConnection conObj = new SqlConnection(con))
{
SqlCommand cmd = new SqlCommand("wsp_Test01", conObj);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
var model = new List<EmpListModel>();
using (SqlConnection conn = new SqlConnection(con))
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(); //it says this is the error
while (rdr.Read())
{
var emp = new EmpListModel();
emp.BioID = Convert.ToString(rdr["BioID"]);
emp.Fullname = Convert.ToString(rdr["Fullname"]);
emp.Dthired = Convert.ToString(rdr["Dthired"]);
emp.DeptID = Convert.ToString(rdr["DeptID"]);
emp.Active = Convert.ToString(rdr["Active"]);
emp.NTLogin = Convert.ToString(rdr["NTLogin"]);
model.Add(emp);
}
}
}
return View();
}
}
}
索引.cshtml
@model IEnumerable<AIStestv01.Models.EmpListModel>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table>
<tr>
<th>Bio ID</th>
<th>FUllname</th>
<th>Date Hired</th>
<th>Dept ID</th>
<th>Active</th>
<th>NT LOgin</th>
</tr>
@foreach (var emp in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => emp.BioID)</td>
<td>@Html.DisplayFor(modelItem => emp.Fullname)</td>
<td>@Html.DisplayFor(modelItem => emp.Dthired)</td>
<td>@Html.DisplayFor(modelItem => emp.DeptID)</td>
<td>@Html.DisplayFor(modelItem => emp.Active)</td>
<td>@Html.DisplayFor(modelItem => emp.NTLogin)</td>
</tr>
}
</table>
【问题讨论】:
-
您的存储过程显然有问题。你自己调试过吗?
-
请包含完整的异常详细信息(包括堆栈跟踪)。请附上
wsp_Test01的源代码。 -
等待生病编辑我的帖子
-
如前所述,您的存储过程存在问题。没有该代码,就无法提供帮助。该错误还表明您没有捕获错误,如果您捕获了错误,您将能够看到异常,这将提供更多详细信息。
-
已经编辑完毕
标签: c# sql-server asp.net-mvc stored-procedures model-view-controller