【发布时间】:2013-11-14 11:02:02
【问题描述】:
当单击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;
}
}
【问题讨论】:
-
在 DAL 中填充 Person 时,您是否调试过此代码并逐步执行?
-
是的,它已被填充。我认为演示文稿中的 new Person() 导致文本框出现空值
-
Person 构造函数是否将 Firstname、PersonID 和 Lastname 设置为
string.Empty还是保留为默认的null? -
public int PersonID { get;放; } 我认为它默认为 null