0

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

Cooper
  • 103
  • 3
  • 15
  • 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? – Tim Schmelter May 19 '14 at 10:44
  • First of all, your `description` is a member field, not a property. Secondly - why would you make a method for getting a PUBLIC member? Third: constructing `public Employee ee = new Employee();` is pointless if you're assigning a method result to it. And forth: Duplicated NullReferenceException question. http://stackoverflow.com/q/4660142/1284902 – Tarec May 19 '14 at 10:44
  • And why are they all strings, that's going to give you significant problems for the ones that shouldn't be. Public member variables are just asking for a catalog of bugs as well. – Tony Hopkinson May 19 '14 at 10:47
  • Thank you all, Tarec that helped. I guess i have alot to learn – Cooper May 19 '14 at 11:01

1 Answers1

1

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.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Could you explain a bit more? I can see no difference in this code to what OP posted. – Tarec May 19 '14 at 10:47
  • 1
    @Tarec: i'm advancing the datareader only once to the next record, OP is advancing it twice. – Tim Schmelter May 19 '14 at 10:48
  • thats not exactly it because i am taking data from two different databases, that why the nested while. why did i use while instead of if and why didnt i use datatable, i guess it's a bad habbit. nevermind of it thanks for the help. – Cooper May 19 '14 at 11:04
  • ohhh i juts noticed i am taking the data twice from the same table, its a but, it should be different table- thanks for helping me realizing it – Cooper May 19 '14 at 11:09