简体   繁体   English

用户代码未处理ASP MVC,System.NullReferenceException

[英]ASP MVC, System.NullReferenceException was unhandled by user code

Hy Master, I try to create CRUD in MVS ASP.net, but I have one problem when I build and RUN my program This my model, EmployeeDB : Hy Master,我尝试在MVS ASP.net中创建CRUD,但是在构建和运行程序EmployeeDB时遇到一个问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CRUDAjax.Models
{
    public class EmployeeDB
    {
        //declare connection string
        string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        //Return list of all Employees
        public List<Employee> ListAll()
        {
            List<Employee> lst = new List<Employee>();
            using(SqlConnection con=new SqlConnection(cs))
            {
                con.Open();
                SqlCommand com = new SqlCommand("SelectEmployee",con);
                com.CommandType = CommandType.StoredProcedure;
                SqlDataReader rdr = com.ExecuteReader();
                while(rdr.Read())
                {
                    lst.Add(new Employee { 
                        EmployeeID=Convert.ToInt32(rdr["EmployeeId"]),
                        Name=rdr["Name"].ToString(),
                        Age = Convert.ToInt32(rdr["Age"]),
                        State = rdr["State"].ToString(),
                        Country = rdr["Country"].ToString(),
                    });
                }
                return lst;
            }
        }

        //Method for Adding an Employee
        public int Add(Employee emp)
        {
            int i;
            using(SqlConnection con=new SqlConnection(cs))
            {
                con.Open();
                SqlCommand com = new SqlCommand("InsertUpdateEmployee", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Id",emp.EmployeeID);
                com.Parameters.AddWithValue("@Name", emp.Name);
                com.Parameters.AddWithValue("@Age", emp.Age);
                com.Parameters.AddWithValue("@State", emp.State);
                com.Parameters.AddWithValue("@Country", emp.Country);
                com.Parameters.AddWithValue("@Action", "Insert");
                i = com.ExecuteNonQuery();
            }
            return i;
        }

        //Method for Updating Employee record
        public int Update(Employee emp)
        {
            int i;
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlCommand com = new SqlCommand("InsertUpdateEmployee", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Id", emp.EmployeeID);
                com.Parameters.AddWithValue("@Name", emp.Name);
                com.Parameters.AddWithValue("@Age", emp.Age);
                com.Parameters.AddWithValue("@State", emp.State);
                com.Parameters.AddWithValue("@Country", emp.Country);
                com.Parameters.AddWithValue("@Action", "Update");
                i = com.ExecuteNonQuery();
            }
            return i;
        }

        //Method for Deleting an Employee
        public int Delete(int ID)
        {
            int i;
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlCommand com = new SqlCommand("DeleteEmployee", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Id", ID);
                i = com.ExecuteNonQuery();
            }
            return i;
        }
    }
}

And this my controler HomeController: 这是我的控制器HomeController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CRUDAjax.Models;

namespace CRUDAjax.Controllers
{
    public class HomeController : Controller
    {
        EmployeeDB empDB = new EmployeeDB();
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult List()
        {
            return Json(empDB.ListAll(),JsonRequestBehavior.AllowGet);
        }
        public JsonResult Add(Employee emp)
        {
            return Json(empDB.Add(emp), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetbyID(int ID)
        {
            var Employee = empDB.ListAll().Find(x => x.EmployeeID.Equals(ID));
            return Json(Employee, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Update(Employee emp)
        {
            return Json(empDB.Update(emp), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Delete(int ID)
        {
            return Json(empDB.Delete(ID), JsonRequestBehavior.AllowGet);
        }
    }
}

This the erorr output 这个erorr输出

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=CRUDAjax
  StackTrace:
       at CRUDAjax.Models.EmployeeDB..ctor() in c:\users\jujur\documents\visual studio 2015\Projects\CRUDAjax\CRUDAjax\Models\EmployeeDB.cs:line 15
       at CRUDAjax.Controllers.HomeController..ctor() in c:\users\jujur\documents\visual studio 2015\Projects\CRUDAjax\CRUDAjax\Controllers\HomeController.cs:line 12
  InnerException

: ScreenShot Erorr ScreenShot错误

最好在转换为整数或十进制之前检查是否存在null值,否则请检查数据库ISNULL(@ Variable,0)中的NULL值...由您选择

找不到连接字符串,请仔细检查连接字符串是否存在。

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.NullReferenceException 未被用户代码处理 - System.NullReferenceException was unhandled by user code C#错误? -用户代码未处理System.NullReferenceException - C# Bug? - System.NullReferenceException was unhandled by user code 如何调试此或可能的原因? Moq.Verify上的用户代码未处理System.NullReferenceException - How to debug this or possible causes? System.NullReferenceException was unhandled by user code on Moq.Verify 连接到Com5:未处理System.NullReferenceException - Connecting to Com5: System.NullReferenceException was unhandled 表适配器填充中未处理的System.NullReferenceException - Unhandled System.NullReferenceException in Table Adapter Fill MVC 5 EF 6.1.3 System.NullReferenceException - MVC 5 EF 6.1.3 System.NullReferenceException 用户代码未处理NullReferenceException - NullReferenceException was unhandled by user code WindowsFormsApplication1.exe中发生了类型为&#39;System.NullReferenceException&#39;的未处理异常 - An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe C# Bunifu UI System.NullReferenceException 未处理 - C# Bunifu UI System.NullReferenceException was unhandled .NET WinForms应用程序在退出时由于未处理的System.NullReferenceException崩溃 - .NET WinForms app crashing on exit with unhandled System.NullReferenceException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM