显示变量';的属性
本文关键字:属性 变量 显示 | 更新日期: 2023-09-27 18:04:13
我正试图在标签中的页面上显示我创建的Employee
类的属性,该属性为description
。类CCD_ 3的变量从数据库获取其值。
这是
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";
}
调试器在线路CCD_ 5上停止并且给出相同的错误通知。
问题出在哪里?我该如何解决?感谢的帮助
我假设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;
}
除此之外,我不喜欢这样的数据库助手类,尤其是在ASP.NET中。你还应该使用sql参数来防止sql注入。