0

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

Fadi
  • 1,997
  • 6
  • 27
  • 68
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ondrej Tucny Apr 13 '14 at 22:47
  • i think this tow question are different cause i'm trying to retrieve data and every thing work expect this details method the Index and Create and Edit work fine – Fadi Apr 13 '14 at 22:58
  • 1
    Well if the Company.find() method can return a null value, then you need to check for this and not try to reference the result. – OldProgrammer Apr 13 '14 at 23:17

2 Answers2

3

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;
Raja Nadar
  • 9,006
  • 2
  • 28
  • 39
  • The two given answers are both correct but my vote goes to this one as it incorporates just a little bit less code (don't see any other major difference?). Also the use of extension methods is more compact here (which also referes back to the first point). – reaper_unique Apr 14 '14 at 06:53
  • There is a difference, because the Find extension method works well when you have an attribute [key] on your `id` in this case `CompanyID` in the model. But in the model there isn't a [Key] attribute on CompanyID. Means these two answers are not the same. –  Apr 14 '14 at 09:46
1

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.