【问题标题】:Showing a variable's property in a label在标签中显示变量的属性
【发布时间】:2014-05-19 18:45:48
【问题描述】:

我试图在我的页面上的标签中显示我创建的类的属性Employee- 该属性是descriptionEmployee 类的变量从数据库中获取它的值。

这里是类:

    public class Employee : User
{
    public string Gender;
    public string DateOfBirth;
    public string EducationYears;
    public string Field;
    public string SubField;
    public string WorkAt;
    public string description;
    public string resume;
    public string word;

    public Employee() : base() { }
    public Employee(string Gender,string DateOfBirth, string EducationYears, string Field, string SubField, string WorkAt, string description, string resume, string word,
         string email, int status, string name, string photo) 
        :base(email, status, name, photo)
    {
        this.Gender=Gender;
        this.DateOfBirth=DateOfBirth;
        this.EducationYears=EducationYears;
        this.Field=Field;
        this.SubField=SubField;
        this.WorkAt=WorkAt;
        this.description = description;
        this.resume = resume;
        this.word = word;
    }
    public string getDescription()
    {
        return this.description;
    }
}

这里是用户:

public class User
{
    public string email;
    public int status;
    public string name;
    public string photo;

    public User() { }
    public User(string email, int status, string name, string photo)
    {
        this.email = email;
        this.status = status;
        this.name = name;
        this.photo = photo;
    }
}

这是我的 asp.net:

public Employee ee = new Employee();


protected void Page_Load(object sender, EventArgs e)
{
    User = "xxx";
    ee = getEmployee(User);
}

这里是getEmployee(string User):

public Employee getEmployee(string email)
{
    DataTableReader r = DAL.getReader("SELECT * FROM Users WHERE Email='" + email + "'");
    while (r.Read())
    {
        DataTableReader ee = DAL.getReader("SELECT * FROM Users WHERE Email='" + email + "'");
        while (r.Read())
        {
            return (new Employee(r["Gender"].ToString(), r["DateOfBirth"].ToString(), r["EducationYears"].ToString(), r["field"].ToString(), r["subField"].ToString(), r["WorkAT"].ToString(), r["description"].ToString(), r["Resume"].ToString(), r["word"].ToString(), r["Email"].ToString(), int.Parse(ee["Status"].ToString()), r["Fname"].ToString() + r["Lname"].ToString(), ee["photo"].ToString()));
        }
    }
    return null;
}

我的html:

   <asp:Label ID="Label1" runat="server" Text='<%=ee.description %>'></asp:Label>

我运行调试器,它停在 html 行 ^ 并给了我错误:

System.NullReferenceException: Object reference not set to an instance of an object.

当我尝试这样做时:

    public Employee ee = new Employee();


protected void Page_Load(object sender, EventArgs e)
{
    User = "xxx";
    ee.description="something";
}

调试器停在ee.description="something"; 行并给出了同样的错误提示。

有什么问题,我该如何解决?感谢您的帮助

【问题讨论】:

  • 我假设getEmployee 返回null。我必须承认我不知道你在那里做什么。为什么不简单地使用DataAdapter 来填充DataTable,或者只使用单个while 而不是嵌套的?
  • 首先,您的description 是成员字段,而不是属性。其次 - 你为什么要制定一种获得 PUBLIC 成员的方法?第三:如果您要为其分配方法结果,则构造 public Employee ee = new Employee(); 是没有意义的。第四:重复的 NullReferenceException 问题。 stackoverflow.com/q/4660142/1284902
  • 为什么它们都是字符串,这会给你不应该出现的问题带来重大问题。公共成员变量也只是要求提供错误目录。
  • 谢谢大家,帮助的 Tarec。我想我还有很多东西要学

标签: c# html asp.net


【解决方案1】:

我假设getEmployee 返回null。我必须承认我不知道你在那里做什么。为什么不简单地使用DataAdapter 来填充DataTable,或者只使用单个while 而不是嵌套的?

public Employee getEmployee(string email)
{
    DataTableReader r = DAL.getReader("SELECT * FROM Users WHERE Email='" + email + "'");
    if(r.Read())
    {
        return (new Employee(r["Gender"].ToString(), r["DateOfBirth"].ToString(), r["EducationYears"].ToString(), r["field"].ToString(), r["subField"].ToString(), r["WorkAT"].ToString(), r["description"].ToString(), r["Resume"].ToString(), r["word"].ToString(), r["Email"].ToString(), int.Parse(r["Status"].ToString()), r["Fname"].ToString() + r["Lname"].ToString(), r["photo"].ToString()));
    }
    return null;
}

除了I am no fan of 这样的数据库助手类,在 ASP.NET 中更是如此。您还应该使用 sql-parameters 来防止 sql 注入。

【讨论】:

  • 您能再解释一下吗?我看不出这段代码与 OP 发布的内容有什么区别。
  • @Tarec:我只将数据阅读器推进一次到下一条记录,OP 推进了两次。
  • 那不完全是因为我从两个不同的数据库中获取数据,这就是为什么嵌套 while。为什么我使用 while 而不是 if 以及为什么我不使用数据表,我想这是一个坏习惯。没关系,谢谢你的帮助。
  • 哦,我注意到我从同一个表中获取了两次数据,但是,它应该是不同的表 - 感谢您帮助我实现它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 2017-07-30
  • 2012-07-12
  • 2020-10-24
相关资源
最近更新 更多