Faabass
Faabass

Reputation: 1164

Some properties from database are null when they have value

I'm trying to make works a login functionality but for some users Linq return some values as null when they have a correct value in database. For other users everything works ok.

This is my linq

public Clientes Login(string username) 
{
    DataClasses1DataContext db = new DataClasses1DataContext();
    var query = from c in db.Clientes
                where c.NroDocumento == username
                select c;
    return query.FirstOrDefault();
}

Then in the service layer I have the following:

Clientes o = userData.Login(username);
return (o != null && password == o.Password) 
    ? new UserEntity(o.CodCliente, o.Email, o.Empresa) 
    : null;

But when I debug, for some users I can see that the property o.Password has a value, and for others it appears as null.

The users are being created by other systems, so I don't know if that can impact, but when I run the Select directly in SQL Server, both users has a Password, so I don't understand which is the difference that makes Linq return null.

This is the image of the model (sorry for the spanish)

enter image description here

Upvotes: 0

Views: 323

Answers (1)

sr28
sr28

Reputation: 5156

As per the comments it looks like there are duplicates, but some of them don't have a password. As you're using 'FirstOrDefault' in some cases it's picking up the one without a password.

Upvotes: 1

Related Questions