-1

Am getting this error when l tried to add data in my project.it point me to this code:

var currentuserid = WebSecurity.CurrentUserId;
var user = db.UserProfiles.SingleOrDefault(u => u.UserId == currentuserid);
List<Claim> claims = db.Claims.Include("Claim_Status").Include("Patient").Include("Hospital").ToList();
var userClaims = claims.Where(c => c.Hospitalid.Value == user.Hospitalid);
return View(userClaims.ToList());

how can l solve this problem.

am using Microsoft sql server 2008.

Umar Bachena
  • 67
  • 1
  • 2
  • 6

1 Answers1

1

One or more of your HospitalIds are null. You can fix it by changing your Where clause to:

var userClaims = claims.Where(c => c.Hospitalid == user.Hospitalid);

which will filter out any claims with a null HospitalId.

Note that you're currently loading the entire Claims table and filtering the collection in memory. You probably want to execute the filter on the database, which you can do with:

List<Claim> claims = db.Claims.Include("Claim_Status").Include("Patient").Include("Hospital")
    .Where(c => c.HospitalId == user.Hospitalid)
    .ToList();
Lee
  • 142,018
  • 20
  • 234
  • 287