bohunn
bohunn

Reputation: 81

WCF Service operation always returns false

I have a problem with WCF Service operation. I get passwod value from database and when it should pass it return false value. What am I doing wrong?

public bool LogIn(string userId, string passwd)
    {
        bool prompt;
        ProgDBEntities context = new ProgDBEntities();

        IQueryable<string> haslo = (from p in context.UserEntity where p.UserID == userId select p.Passwd);


        bool passOk = String.Equals(haslo, passwd);


        if (passOk == true )
        {
            prompt = true;                
        }
        else
        {
            prompt = false;               
        }
        return prompt;
    }

Upvotes: 1

Views: 450

Answers (2)

MrNick
MrNick

Reputation: 399

haslo represents a collection of strings, not an individual string. This means String.Equals(haslo, passwd) will always return false since you're comparing a collection of strings to an individual string - they're two different types of objects.

You could try modifying the code as follows. FirstOrDefault() will return the first string in the collection, or NULL if it's empty.

bool passOk = String.Equals(haslo.FirstOrDefault(), passwd);

Upvotes: 2

Steven Magana-Zook
Steven Magana-Zook

Reputation: 2759

It seems like you want to compare a single retrieved entry with the password that was passed in (as opposed to any IQueryable/IEnumerable). For that, try using the FirstOrDefault method:

public bool LogIn(string userId, string passwd)
    {
        bool prompt;
        ProgDBEntities context = new ProgDBEntities();

        var haslo = (from p in context.UserEntity where p.UserID == userId select p.Passwd).FirstOrDefault();

        // No need to use String.Equals explicitly
        bool passOk = haslo == passwd;


        if (passOk == true )
        {
            prompt = true;                
        }
        else
        {
            prompt = false;               
        }
        return prompt;
    }

Upvotes: 3

Related Questions