使用ADO.Net核心与模型,我想显示来自不同关系表的数据,但它给了我一些错误

时间:2016-06-05 11:22:55

标签: c# asp.net ado.net datareader asp.net-mvc-5

我在下面的代码中有错误。当我将它用于一个表时,它工作正常,但它对关系表不起作用。

显示所有记录的方法:

public List<AdminUserRegisterModel> GetAll()
    {
        List<AdminUserRegisterModel> List = new List<AdminUserRegisterModel>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_admin_user_search_all", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataReader dr;

            connection.Open();
            dr = cmd.ExecuteReader();

           // while(dr.HasRows)
            //{
                while(dr.Read())
                {
                    AdminUserRegisterModel obj = new AdminUserRegisterModel();
                    obj.adminUsers.adminId = Convert.ToInt32(dr["adminId"]);
                    obj.adminUsers.adminName = Convert.ToString(dr["adminName"]);
                    obj.adminUsers.adminEmail = Convert.ToString(dr["adminEmail"]);
                    obj.adminUsers.adminPassword = Convert.ToString(dr["adminPassword"]);
                    obj.adminUsers.adminStatus = Convert.ToInt32(dr["adminStatus"]);
                    obj.adminUsers.joinDate = Convert.ToDateTime(dr["adminJoinDate"]);
                    // AdminUserProfile table
                    obj.adminProfiles.adminProfileId = Convert.ToInt32(dr["profileId"]);
                    obj.adminProfiles.firstName = Convert.ToString(dr["firstName"]);
                    obj.adminProfiles.lastName = Convert.ToString(dr["lastName"]);
                    obj.adminProfiles.cnic = Convert.ToString(dr["cnic"]);
                    obj.adminProfiles.profileImg = Convert.ToString(dr["profileImg"]);
                    obj.adminProfiles.adminId = Convert.ToInt32(dr["adminId"]);

                    List.Add(obj);
                }
            //}

            if (!dr.IsClosed)
            {
                dr.Close();
            }
            connection.Close();

            return List;
        }
        catch
        {
            connection.Close();
            return null;
        }
        finally
        {
            connection.Close();
        }
    }

控制器方法:

// Display All Records
    public ActionResult Display()
    {
        repObj = new AdminUserRegisterRepository();
        return View(repObj.GetAll());
    }

模特课程:

public class AdminUserRegisterModel
{
    public adminUser adminUsers { get; set; }
    public admin_profile adminProfiles { get; set; }
}

它给了我以下错误:

enter image description here

0 个答案:

没有答案