0

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; } } 
6
  • I am getting no always null why do you expect to have always null? Commented Nov 3, 2015 at 14:35
  • Your naming is just perfect ! Commented Nov 3, 2015 at 14:36
  • 1
    I guess a typo as the question is "I'm getting always null" @Thomas Commented Nov 3, 2015 at 14:36
  • @kymberly At the line "var myUser = soccerEntit " are you sure that soccerEntities contains data? Commented Nov 3, 2015 at 14:36
  • If you're getting null in that LINQ query then that would mean there are no elements in the collection matching that condition. Why do you expect that not to be the case? Why should a new instance of soccerEntities contain a matching element? Commented Nov 3, 2015 at 14:38

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

The compare of the username can fail on casing.
Indeed. There are multiple reasons why it could fail. Casing, Cultural, Whitespaces or New lines. Without the data we only can advice to use another way of comparing.
The problem is also BOTH sides could have unexpected data inside (the database data and also the userName and password of the method).
@Thomas, this can also be the case. I like to add (i cannot comment on you yet..) That you should use the equals method from the string object like so: string.Equals(value1,value2); in this way there is no NullPointerException.
@RobbertBrussaard Good point there although that should only occur if he has users with username or password being null (which should not be the case if the database is filled ina sense making way but it should be addressed. will put that into an edit tnx!)
|
1

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.

Comments

0

I would use:

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

6 Comments

@KirkWoll because is provide a working version of what he asks?
No, it's providing an equivalent solution in a different style.
@Thomas: It refactors the existing logic. But if the existing logic isn't working as expected, then this won't do much.
@David the OP expect the default value to be null which seems to be/is not the case.
The question is though if its the logic or the data. From what I've seen in the question it points more into the data direction as firstordefault SHOULD theoretically work as intended (although I will never get why ppl use == instead of .Equals but that is a different thing).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.