0

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?

2
  • Is row.UserProfile or row.UserProfile.DoctorDetail null ? Commented Jul 8, 2018 at 16:50
  • You have to Check UserProfiler is not null. @if (@row.UserProfile !=null) { —- write your code here } Commented Jul 8, 2018 at 16:52

1 Answer 1

1

I Found Answer , i was edit my code to

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

Fixed error

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.