当我点击gridview中的linkbutton然后获取其关联数据并显示在模态内的文本框上时,我试图填充Person对象。我能够获取数据并将其传递给Person对象,但是,当我填充模态控件时,Person对象在所有属性上都返回null。
任何想法如何将Person对象的值传递给文本框控件而不调用Person objPerson = new 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;
}
BusinessLogicLayer:
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;
}
}
答案 0 :(得分: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;
}
}
答案 1 :(得分: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;
}