Non static method requires a target

asp.net c# entity-framework

Question

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

1
5
8/20/2016 5:52:52 PM

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

8
9/30/2013 12:56:17 PM

Popular Answer

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;
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow