dotnetdevcsharp
dotnetdevcsharp

Reputation: 3980

Always returning null

For Some reason I am getting no always null in this query I have no idea why I have debuged and I am getting no values but yet my datagrids are fine and showing users.

public Boolean VerifyPassword(string userName, string password)
{
    //The ".FirstOrDefault()" method will return either the first matched
    //result or null
    var myUser = soccerEntities.Users
        .FirstOrDefault(u => u.UserName == userName
                     && u.password == password);

    if (myUser == null)    //User was not found
    {
        //Proceed with your login process...
        return false;
    }
    else    //User was  found
    {

        return true;        
        //Do something to let them know that their credentials were not valid
    }
}

This is my soccer entites

private soccerEntities _soccerEntities;
protected soccerEntities soccerEntities
{
    get
    {
        if (_soccerEntities == null)
        {
            try
            {
                _soccerEntities = new soccerEntities();
            }
            catch (Exception ex)
            {
                throw new EntityContextException("Soccer Entities Could not be created", ex);
            }
        }

        return _soccerEntities;
    }
}

Upvotes: 0

Views: 132

Answers (3)

Thomas
Thomas

Reputation: 2984

From what it seems in the question I'm taking it that the method always ends with false, thus:

  var myUser = soccerEntities.Users
        .FirstOrDefault(u => u.UserName == userName
                     && u.password == password);

sets myUser to null.

If that is the case, then it points into the direction of the data instead of the logic. Thus I would strongly advice to do the following things: 1.) Check if the soccerEntities.Users is filled when you reach that line 2.) Check if there are any filler characters attached to either (blanks for example if the database table coloumn is of type char instead of varchar).

One thing though in addition is you should use .Equals instead of ==.

For example if you want to make sure that no blanks are "tainting" the result you could test the following and remove the .Trim() vom userName.Trim() and password.Trim() later on:

  var myUser = soccerEntities.Users
        .FirstOrDefault(u => u.UserName.Trim().Equals(userName.Trim())
                     && u.password.Trim().Equals(password.Trim()));

But as said currently all you said points into a data problem, so that either the data you have is empty OR somehow not what you expect (like leading or trailing blanks)

And in case the problem is big and small letters you should convert them to one type of letters for example by using u.password.Trim().ToUpper() to convert all letters to their big letter aequivalent.

Edit: Of note: The above things only work if the database and also uername, password are not null. Then .Trim() fails and u.UserName.Equals( also fails. Thus if that COULD be the case you have to make sure that u.Username.Equals is only called if it is not null thus

u.UserName != null && u.UserName.Trim().Equal.......

The same holds true for u.password. If userName and password CAN be null I would set them to String.Empty at the beginning of the method as else it would get really complicated.

Upvotes: 1

Robbert Brussaard
Robbert Brussaard

Reputation: 135

It is most likely that the data that you retrieve does not match with given username and password strings.

Maybe:

  • There is no data;
  • There is white space in the retrieved data;
  • The data is encrypted and the string you use are not encrypted, therefor there is no match.

With no information, the only advice i can give is to adjust the way you compare the string.

Upvotes: 4

Thomas Ayoub
Thomas Ayoub

Reputation: 29451

I would use:

public Boolean VerifyPassword(string userName, string password)
{
    return soccerEntities.Users
        .Any(u => u.UserName == userName
                     && u.password == password);
}

Upvotes: 0

Related Questions