Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中将值从实体传递到表示#_C#_Oop - Fatal编程技术网

C# 如何在c中将值从实体传递到表示#

C# 如何在c中将值从实体传递到表示#,c#,oop,C#,Oop,如何将值从实体层传递到表示层。我已经填充了object Person,但是当我在Presentation中调用它时,它变为null 有人能帮我吗。提前谢谢 实体: public class Person { public int PersonID { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } } 数据层: public List<Pe

如何将值从实体层传递到表示层。我已经填充了object Person,但是当我在Presentation中调用它时,它变为null

有人能帮我吗。提前谢谢

实体:

public class Person
{
    public int PersonID { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}
数据层:

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

    ...

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

您的代码存在一些问题:


  • 为什么
    GetPersonSingleByPersonID(字符串personID)
    返回列表? 如果要返回单个person,则应将方法定义为
    person GetPersonSingleByPersonID(字符串personID)


  • 您根本没有调用
    GetPersonSingleByPersonID(字符串personID)
    方法。相反,您正在创建
    Person
    类(
    Person objPerson=newperson()
    )的新“空白”实例
    您需要调用
    GetPersonSingleByPersonID(id)
    ,因为您返回的是一个列表而不是单个对象(请参见第1点)。您可能需要添加
    .FirstOrDefault()


  • Person objPerson=GetPersonSingleByPersonID(id).FirstOrDefault()

    您需要在数据访问层中调用GetPersonSingleByPersonID()方法。 创建DAL的实例,然后调用该方法。这将返回包含详细信息的所需对象,然后最终将其分配给文本框


    但是请记住,该方法返回一个集合。。。因此,如果您要遍历整个集合并将其写入文本框,那么您将覆盖文本框中以前的值。

    您最好在数据表示之间有一个业务层,您可以从数据层获取实体并对其进行处理,并转换为表示层对象。

    您确定您的
    objPerson
    null
    吗。没有任何内容看起来有误,但您确定文本字段已正确初始化吗?您的代码看起来正确。你确定有行吗?它们确实包含数据吗?调试代码时会发生什么?(您也没有显示实际获取数据的代码部分..这可能也是相关的。)是的,当我在datalayer中调试时会有结果。。但当我试图显示来自对象的数据时,它变为null
    GetPersonSingleByPersonID
    返回一个列表。您是否可能执行了类似于
    var person=(person)GetPersonSingleByPersonID(id)的操作我认为这行Person objPerson=newperson();正在创建另一个为null的Person实例。。如何在不实例化新人的情况下从DAL中检索到演示文稿的值?很抱歉,我将我的方法隐藏在省略号中,以下是我的代码。。List newSiteCompany=新列表();DataTable dt=新的DataTable();SqlParameter[]sqlParam=新的SqlParameter[1];sqlParam[0]=新的SqlParameter(“@CompanyID”,SqlDbType.VarChar);sqlParam[0]。值=公司ID;CommonDAL CommonDAL=新CommonDAL();dt=commonDAL。选择queryproc(“pSiteCompany\u GetSiteCompanySingleByCompanyID”,sqlParam)@devkiat-更新您的问题,上面的代码不可读。
    
    Person objPerson = new Person(); //I think error goes here
    txtPersonID.Text = objPerson.PersonID;
    txtFirstname.Text = objPerson.Firstname;
    txtLastname.Text = objPerson.Lastname;