Ozan
Ozan

Reputation: 85

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

Upvotes: 7

Views: 37465

Answers (3)

Carlos Landeras
Carlos Landeras

Reputation: 11063

"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.

Upvotes: 2

Rom Eh
Rom Eh

Reputation: 2063

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.

Upvotes: 10

Sysketov Alexey
Sysketov Alexey

Reputation: 115

Use

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

Upvotes: 1

Related Questions