Khnemu Khnemu - 3 months ago 10
ASP.NET (C#) Question

Non static method requires a target

private PMS_USERS currUser;
private bool validateUserName()
{
dbContext = new PmsEntities();
var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF).Where(p=> p.USERNAME == currUser.USERNAME);
return !validateUser.Any();
}


Hello,
I got an error while validating on my new user register form.

My
PMS_USERS
table has no record(null). I also tried checking for null control(s) for currUser.

What am I missing?

Error is :


Non static method requires a target

Answer

You should first test if currUser is null or not and your dbContext too.

if (currUser == null) return false;
if (dbContext == null) throw new Exception ("The dbContext has not been set");

Secondly, you can simplify your query like yhat :

 var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF &&  p.USERNAME == currUser.USERNAME);

And then change the return statement to :

return (validateUser.FirstOrDefault() != null);

You can alternativelly use SingleOrDefault statement insead of FirstOrDefault, if you want to be sure there is only one user corresponding to your criteria.

Comments