Reputation: 103
I am trying to show on my page in a label a property of a class i made Employee
- the property is description
.
the variable of the class Employee
gets it's values from a database.
here is the class:
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;
}
}
here is User:
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;
}
}
here is my asp.net:
public Employee ee = new Employee();
protected void Page_Load(object sender, EventArgs e)
{
User = "xxx";
ee = getEmployee(User);
}
here is 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;
}
My html:
<asp:Label ID="Label1" runat="server" Text='<%=ee.description %>'></asp:Label>
i run debugger and it stopped on the html line ^ and gave me the error:
System.NullReferenceException: Object reference not set to an instance of an object.
when i tried to do like this:
public Employee ee = new Employee();
protected void Page_Load(object sender, EventArgs e)
{
User = "xxx";
ee.description="something";
}
the debugger stopped on the line ee.description="something";
and gave the same error notice.
What is the problem and how do i fix it? Thanks for the help
Upvotes: 0
Views: 69
Reputation: 460168
I assume getEmployee
returns null
. I must admit that i don't know what you're doing there. Why don't you simply use a DataAdapter
to fill a DataTable
or just use a single while
instead of the nested one?
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;
}
Apart from that I am no fan of such database helper classes, all the more in ASP.NET. You also should use sql-parameters to prevent sql injection.
Upvotes: 1