在标签中显示变量的属性

时间:2014-05-19 10:39:44

标签: c# html asp.net

我正在尝试在标签页面上显示我所制作的类Employee的属性 - 属性为description。 类Employee的变量从数据库中获取它的值。

这是班级:

    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";行停止并发出相同的错误通知。

问题是什么?如何解决?谢谢你的帮助

1 个答案:

答案 0 :(得分: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; }

{{1}}

除了I am no fan of这样的数据库助手类之外,ASP.NET中还有更多。您还应该使用sql-parameters来阻止sql注入。