[英]Invalid Operation Exception was unhandled by user code
大家好,有人可以在這里給我幫助嗎,我是新手,我不知道該怎么辦! 錯誤是指向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();
}
}
}
Index.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>
在SqlConnection創建中有兩個using語句。 在SqlCommand cmd中,您正在使用第一個連接-conObj,但是使用conn.Open()打開第二個連接。 省略使用的第二個SqlConnection,然后打開第一個。 也可以使用SqlDataReader創建來放入。
編輯:根據NightOwl888的建議,指出未打開連接的問題,這是更改后的代碼。 編輯2:我已經更改了return語句,以將模型列表包括到返回視圖中。
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>();
conObj.Open();//Openning of the connection associated with the sql command
using (SqlDataReader rdr = cmd.ExecuteReader())//Using around the SqlDataReader to dispose it after use
{
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);
}
}
}
//do something with the
return View(model);//added model to use it in cshtml
}
}
}
首先,您不需要兩個連接即可執行此操作。 單個連接即可解決問題。 其次,問題似乎與查詢返回的字段的數據類型有關。 嘗試使用“?.ToString()”代替該Convert.ToString。 另外,請考慮處理來自數據庫的數據類型。 例如,DateTime列可以在C#中轉換為DateTime,然后可以使用ToString('format')提取值。
另一個建議是從Data Reader循環中刪除所有列,並開始一個一個地添加,這樣您就可以隔離引起問題的列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.