用户代码未处理无效的操作异常

JM

大家好,有人可以在这里给我帮助我是新手,我不知道该怎么办!错误指向 SqlDataReader rdr = cmd.ExecuteReader() 它说,

System.Data.dll 中出现类型为“System.InvalidOperationException”的异常,但未在用户代码中处理

我的代码在下面。提前致谢。

员工列表模型

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; }
    }
}

员工控制器

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>
托马斯

您有两个 using 语句与 SqlConnection 创建。在您的 SqlCommand、cmd 中,您使用的是第一个连接 - conObj,但您使用 conn.Open() 打开了第二个连接。省略第二个 SqlConnection using,并打开第一个。也可以使用 SqlDataReader 创建。

编辑:正如 NightOwl888 所建议的指出未打​​开连接的问题,这是更改后的代码。编辑 2:我已更改返回语句以将模型列表包含到返回视图中。

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
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

用户代码未处理 SQL 异常

来自分类Dev

用户代码在连接打开时未处理 SQL 异常

来自分类Dev

用户未处理MOQ异常

来自分类Dev

用户代码未处理CryptographicException

来自分类Dev

ModelValidationException是未处理的用户代码

来自分类Dev

用户代码未处理CryptographicException

来自分类Dev

无效的转换异常未处理的MySQL

来自分类Dev

未处理无效的操作异常-无法一次更新2件事情

来自分类Dev

在Cloud Firestore中添加文档时出错:未处理的异常:无效的参数:“用户”的实例

来自分类Dev

用户回调中未处理的异常

来自分类Dev

在.Where()中使用.SingleOrDefault()将引发以下异常:-用户代码未处理System.NotSupportedException

来自分类Dev

.NET SqlDataReader:用户代码未处理SqlException

来自分类Dev

for循环中的用户代码未处理IndexOutOfRangeException

来自分类Dev

用户代码未处理C#FileNotFoundException

来自分类Dev

ProviderIncompatibleException由用户代码未处理

来自分类Dev

for循环中的用户代码未处理IndexOutOfRangeException

来自分类Dev

错误:用户代码未处理 XmlException

来自分类Dev

未处理的异常呈现组件:无法构造“ URL”:无效的URL

来自分类Dev

Visual Studio 2013不会引发用户未处理的异常

来自分类Dev

为什么此代码中存在未处理的异常?

来自分类Dev

序列化代码导致未处理的异常

来自分类Dev

任务未处理的异常

来自分类Dev

未处理格式异常

来自分类Dev

Java:未处理的异常

来自分类Dev

发生未处理的异常

来自分类Dev

Android未处理的异常

来自分类Dev

未处理的异常:NoSuchMethodException

来自分类Dev

奇怪的错误:-用户代码未处理System.Web.HttpCompileException

来自分类Dev

执行期间用户代码错误未处理SqlException

Related 相关文章

  1. 1

    用户代码未处理 SQL 异常

  2. 2

    用户代码在连接打开时未处理 SQL 异常

  3. 3

    用户未处理MOQ异常

  4. 4

    用户代码未处理CryptographicException

  5. 5

    ModelValidationException是未处理的用户代码

  6. 6

    用户代码未处理CryptographicException

  7. 7

    无效的转换异常未处理的MySQL

  8. 8

    未处理无效的操作异常-无法一次更新2件事情

  9. 9

    在Cloud Firestore中添加文档时出错:未处理的异常:无效的参数:“用户”的实例

  10. 10

    用户回调中未处理的异常

  11. 11

    在.Where()中使用.SingleOrDefault()将引发以下异常:-用户代码未处理System.NotSupportedException

  12. 12

    .NET SqlDataReader:用户代码未处理SqlException

  13. 13

    for循环中的用户代码未处理IndexOutOfRangeException

  14. 14

    用户代码未处理C#FileNotFoundException

  15. 15

    ProviderIncompatibleException由用户代码未处理

  16. 16

    for循环中的用户代码未处理IndexOutOfRangeException

  17. 17

    错误:用户代码未处理 XmlException

  18. 18

    未处理的异常呈现组件:无法构造“ URL”:无效的URL

  19. 19

    Visual Studio 2013不会引发用户未处理的异常

  20. 20

    为什么此代码中存在未处理的异常?

  21. 21

    序列化代码导致未处理的异常

  22. 22

    任务未处理的异常

  23. 23

    未处理格式异常

  24. 24

    Java:未处理的异常

  25. 25

    发生未处理的异常

  26. 26

    Android未处理的异常

  27. 27

    未处理的异常:NoSuchMethodException

  28. 28

    奇怪的错误:-用户代码未处理System.Web.HttpCompileException

  29. 29

    执行期间用户代码错误未处理SqlException

热门标签

归档