Saeed Gholampoor
Saeed Gholampoor

Reputation: 11

ASP.NET MVC code first related table null reference

I try to fetch user list from data base but some times 1 related table I call it is null and have not value for some user, so I get a null reference error.

@if (@row.UserProfile.DoctorDetail.Id != null) {
    @row.UserProfile.DoctorDetail.Expertise
}

ASP.NET MVC returns this error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

So how can I check related table for null reference?

Upvotes: 0

Views: 96

Answers (1)

Saeed Gholampoor
Saeed Gholampoor

Reputation: 11

I Found Answer , i was edit my code to

 @if (@row.UserProfile.DoctorDetail != null)
 {
 @row.UserProfile.DoctorDetail.Expertise
 }

Fixed error

Upvotes: 1

Related Questions