从数据库访问数据时出现空引用异常

时间:2018-03-24 22:33:02

标签: c# asp.net sql-server crud

我正在尝试在ASP.NET MVC中实现CRUD操作,但是当我尝试访问包含单个表的数据库时,我收到错误Null Reference Exception。

这是与数据库相关的操作的OfficeDB.c:

pair<const long, vector<double>> mypair{ 20L, {2.3, -5.1} };
auto xxx = find(stocks.begin(), stocks.end(), mypair);
...if xxx != stocks.end(), *xxx is the matching pair in stocks...

我在这一行得到了错误按摩

namespace ArfniAdmin.Models



   public class OfficeDB
{
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

    //Return list of all Offices  
    public List<Office> ListAll()
    {
        List<Office> lst = new List<Office>();
        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 Office
                {
                    id = Convert.ToInt32(rdr["Id"]),
                    Name = rdr["Office_Name"].ToString(),
                    Location = rdr["Office_Loaction"].ToString(),
                    Descreption = rdr["Office_Descreption"].ToString(),
                    Phone = rdr["Office_Phone"].ToString(),
                });
            }
            return lst;
        }

    }

    //Method for Adding an Office  
    public int Add(Office office)
    {
        int i;
        using (SqlConnection con = new SqlConnection(cs))
        {
            con.Open();
            SqlCommand com = new SqlCommand("InsertUpdateOffice", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@Id", office.id);
            com.Parameters.AddWithValue("@Name", office.Name);
            com.Parameters.AddWithValue("@Location", office.Location);
            com.Parameters.AddWithValue("@Descreption", office.Descreption);
            com.Parameters.AddWithValue("@Phone", office.Phone);
            com.Parameters.AddWithValue("@Action", "Insert");
            i = com.ExecuteNonQuery();
        }
        return i;
    }

    //Method for Updating Office record  
    public int Update(Office office)
    {
        int i;
        using (SqlConnection con = new SqlConnection(cs))
        {
            con.Open();
            SqlCommand com = new SqlCommand("InsertUpdateOffice", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@Id", office.id);
            com.Parameters.AddWithValue("@Name", office.Name);
            com.Parameters.AddWithValue("@Location", office.Location);
            com.Parameters.AddWithValue("@Descreption", office.Descreption);
            com.Parameters.AddWithValue("@Phone", office.Phone);
            com.Parameters.AddWithValue("@Action", "Update");
            i = com.ExecuteNonQuery();
        }
        return i;
    }

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

问题的可能原因是什么?

0 个答案:

没有答案