Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 660
Ozan Avatar asked Sep 30 '13 12:09

Ozan


2 Answers

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.

like image 133
Rom Eh Avatar answered Sep 23 '22 12:09

Rom Eh


"Non static method requires a target" means that some object inside the scope is null.

Try checking the context and the var result values:

 dbContext = new PmsEntities();
 if (dbContext != null && currUser != null)
 {
     var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF && p.USERNAME == currUser.USERNAME);

    if (validateUser !=null)
    {
       return !validateUser.Any();
    }
    else
       return null;
 }

Check it and tell us if you have the same exception.

like image 22
Carlos Landeras Avatar answered Sep 19 '22 12:09

Carlos Landeras