如何传递从实体层值presentation层 [英] How can I pass values from Entity Layer to Presentation Layer

查看:108
本文介绍了如何传递从实体层值presentation层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来填充Person对象时,点击里面的GridView的LinkBut​​ton,然后取上一个模式里面的文本及其相关的数据和显示。我能够读取数据并将它传递给Person对象,但是,当我填充我的模态控制,Person对象上的所有属性返回null。

任何想法吨如何通过Person对象为文本框控件的值,而无需调用人objPerson =新的Person()?

感谢您提前。

实体:

 公共类Person
{
    公众诠释是PersonID {搞定;组; }
    公共字符串名字{获得;组; }
    公共字符串姓氏{搞定;组; }
}

数据层:

 公开数据表SelectQueryProc(字符串_storedProc,的SqlParameter []的SqlParameter)
{
    数据表T =新的DataTable();    尝试
    {
        使用(SqlConnection的SqlConnection的=新的SqlConnection(CommonEntity.ConnectionString))
        {
            sqlConnection.Open();
            使用(SqlDataAdapter的适配器=新SqlDataAdapter的(_storedProc,SqlConnection的))
            {
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand.Parameters.AddRange(的SqlParameter);
                adapter.Fill(T);
            }
        }
    }
    赶上(SQLEXCEPTION E)
    {
        抛出新SystemException的(e.ToString());
    }
    最后
    {    }
    返回吨;
}公开名单<&人GT; GetPersonSingleByPersonID(字符串PERSONID)
{
    清单<&人GT; objPerson =新的List<&人GT;();
    DataTable的DT =新的DataTable();    的SqlParameter [] sqlParam =新的SqlParameter [1];
    sqlParam [0] =新的SqlParameter(@是PersonID,SqlDbType.VarChar);
    sqlParam [0] .value的PERSONID =;    CommonDAL commonDAL =新CommonDAL();
    DT = commonDAL.SelectQueryProc(GetPersonSingleByPersonID,sqlParam);    的foreach(在dt.Rows的DataRow博士)
    {
        objPerson.Add(新的Person()
        {
            是PersonID =博士[是PersonID]。的ToString()
            名字=博士[名字]。的ToString()
            姓=博士[姓氏。的ToString()
        });
    }
    返回objPerson;
}

BusinessLogicLayer:

 公开名单<&人GT; GetPersonSingleByPersonID(字符串PERSONID)
{
    PersonDAL objPersonDAL =新PersonDAL();
    返回objPersonDAL.GetPersonSingleByPersonID(companyID);
}

presentationLayer:

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    开关(e.CommandName)
    {
        案点击:
            {
                //填充人员详细信息
                PersonBL objPersonBL =新PersonBL();
                objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());                人objPerson =新的Person();
                txtPersonID.Text = objPerson.PersonID;
                txtFirstname.Text = objPerson.Firstname;
                txtLastname.Text = objPerson.Lastname;
                打破;
            }
        默认:
        打破;
    }
}


解决方案

您尝试从空Person对象获取值,而不是从你的DAL。所以,你的方法应该是这样的。

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
        {
            开关(e.CommandName)
            {
                案点击:
                    {
                        //填充人员详细信息
                        PersonBL objPersonBL =新PersonBL();
                        变种objPerson = objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());                        //人objPerson =新的Person();
                        如果(objPerson.Count!= 0)
                        {
                            txtPersonID.Text = objPerson [0] .PersonID;
                            txtFirstname.Text = objPerson [0] .Firstname;
                            txtLastname.Text = objPerson [0] .Lastname;
                            打破;
                        }
                    }
                默认:
                    打破;
            }
        }

I am trying to populate Person object when linkbutton inside gridview is clicked then fetch its associated data and display on the textbox inside a modal. I am able to fetch the data and pass it to Person object, however, when I populate my modal controls, Person object returns null on all properties.

Any idea ton how to pass the value of Person object to textbox controls without without calling Person objPerson = new Person()?

Thank you in advance.

Entity:

public class Person
{
    public int PersonID { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

DataLayer:

public DataTable SelectQueryProc(String _storedProc, SqlParameter[] sqlParameter)
{
    DataTable t = new DataTable();

    try
    {
        using (SqlConnection sqlConnection = new SqlConnection(CommonEntity.ConnectionString))
        {
            sqlConnection.Open();
            using (SqlDataAdapter adapter = new SqlDataAdapter(_storedProc, sqlConnection))
            {
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand.Parameters.AddRange(sqlParameter);
                adapter.Fill(t);
            }
        }
    }
    catch (SqlException e)
    {
        throw new SystemException(e.ToString());
    }
    finally
    {

    }
    return t;
}

public List<Person>GetPersonSingleByPersonID(string personID)
{
    List<Person> objPerson = new List<Person>();
    DataTable dt = new DataTable();

    SqlParameter[] sqlParam = new SqlParameter[1];
    sqlParam[0] = new SqlParameter("@PersonID", SqlDbType.VarChar);
    sqlParam[0].Value = personID;

    CommonDAL commonDAL = new CommonDAL();
    dt = commonDAL.SelectQueryProc("GetPersonSingleByPersonID", sqlParam);

    foreach (DataRow dr in dt.Rows)
    {
        objPerson.Add(new Person()
        {
            PersonID = dr["PersonID"].ToString(),
            Firstname = dr["Firstname"].ToString(),
            Lastname = dr["Lastname"].ToString()            
        });
    }
    return objPerson;
}

BusinessLogicLayer:

public List<Person> GetPersonSingleByPersonID(string personID)
{
    PersonDAL objPersonDAL = new PersonDAL();
    return objPersonDAL.GetPersonSingleByPersonID(companyID);
}

PresentationLayer:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "click":
            {
                // Populate Person Details                        
                PersonBL objPersonBL = new PersonBL();
                objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());

                Person objPerson = new Person();
                txtPersonID.Text = objPerson.PersonID;
                txtFirstname.Text = objPerson.Firstname;
                txtLastname.Text = objPerson.Lastname;
                break;
            }
        default:
        break;
    }
}

解决方案

You try to get values from empty Person object, not from your DAL. So your method should be like this

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "click":
                    {
                        // Populate Person Details                        
                        PersonBL objPersonBL = new PersonBL();
                        var objPerson = objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());

                        //Person objPerson = new Person();
                        if (objPerson.Count != 0)
                        {
                            txtPersonID.Text = objPerson[0].PersonID;
                            txtFirstname.Text = objPerson[0].Firstname;
                            txtLastname.Text = objPerson[0].Lastname;
                            break;
                        }
                    }
                default:
                    break;
            }
        }

这篇关于如何传递从实体层值presentation层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆