Fadi
Fadi

Reputation: 2370

Object reference not set to an instance of an object when retrieving data from data base

i'm having an issue when i try to retrieve data from the database by the Details action method and i have one to many relationship between to tables Company and UserProfile and i'm using the entity framework my models are the UserProfile

[Table("UserProfile")]
public class UserProfile
{

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }
    public string UserName { get; set; }

    public Membership Membership { get; set; }
    public ICollection<Company> Companys { get; set; }
    public ICollection<UsersInRoles> UsersInRoles { get; set; }
    public ICollection<Comment> Comments { get; set; }
    public ICollection<Like> Likes { get; set; }

}

and the Company

[Bind(Exclude = "CompanyID")]
public class Company
{
    [ScaffoldColumn(false)]
    public int CompanyID { get; set; }

    [Required(ErrorMessage = "Company Name is required")]
    [DisplayName("Company Name")]
    [StringLength(40)]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "Mobile Number is required")]
    [DisplayName("Mobile Number")]
    //[DataType(DataType.PhoneNumber)]
    public string CompanyPhoneNumber { get; set; }

    [Required(ErrorMessage = "Company Address is required")]
    [DisplayName("Company Address")]
    [StringLength(128)]
    public string CompanyAddress { get; set; }

    //must change in controller to view only the manager user
    [Required(ErrorMessage = "Manager Name is required")]
    [DisplayName("Manager")]
    public int UserID { get; set; }
    public UserProfile UserProfile { get; set; }

    public ICollection<Store> Stores { get; set; }
    public ICollection<Product> Products { get; set; }
}

the controller and Details action method

public ActionResult Details(int id = 0)
    {
        Company company = db.Companys.Find(id);
        string user = company.UserProfile.UserName;
        if (company == null)
        {
            return HttpNotFound();
        }
        return View(company);
    }

i set the string user = company.UserProfile.UserName; and set a break point to it to see what is the value that is returned and its null every time and it give me this error Object reference not set to an instance of an object so what i'm doing wrong thanks for any help

Upvotes: 0

Views: 476

Answers (2)

user3383479
user3383479

Reputation:

Try this:

public ActionResult Details(int id = 0)
{
    Company company = db.Companys.Where(c=>c.CompanyID ==id).Select(c=>c);
   //you can check if the value is null after
    if(company!=null)
    string user = company.UserProfile.UserName;
    else
    {
        return HttpNotFound();
    }
    return View(company);
}

Hope it will help you.

Upvotes: 1

Raja Nadar
Raja Nadar

Reputation: 9499

Your company check needs to be before the UserName access as follows:

Company company = db.Companys.Find(id);

// do the null check first.
if (company == null)
{
 return HttpNotFound();
}

string user = company.UserProfile.UserName;

Upvotes: 3

Related Questions