0

当单击gridview 中的链接按钮时,我试图填充Person 对象,然后获取其关联数据并显示在模式内的文本框中。我能够获取数据并将其传递给 Person 对象,但是,当我填充我的模态控件时,Person 对象在所有属性上都返回 null。

知道如何在不调用 Person objPerson = new Person() 的情况下将 Person 对象的值传递给文本框控件吗?

先感谢您。

实体:

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

数据层:

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

业务逻辑层:

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

表示层:

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

2 回答 2

2

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

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;
            }
        }
于 2013-11-14T11:16:11.890 回答
0

很明显,您正在将新创建的对象(尚未填充)值分配给文本框。您的代码应该以某种方式:

PersonBL objPersonBL = new PersonBL();
Person objPerson = objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());
 if (objPerson.Count != 0)
{
txtPersonID.Text = objPerson.PersonID;
txtFirstname.Text = objPerson.Firstname;
txtLastname.Text = objPerson.Lastname;
}
于 2013-11-14T11:30:58.730 回答