0

I used linq to sql many times its work correctly but today something wrong

this is code which must select password from users table. I call this method tell wright... result is not password please see picture.

public void GetPassword(int id) 
    {
        using (HProDataContext db = new HProDataContext())
        {
            _CurrentPassword = (from p in db.users
                                    where p.id == _CurrentID
                                    select p.password).ToString();
        }
    }

this how i use this method

private void btnOK_Click(object sender, RoutedEventArgs e)
    {
        if (listView1.SelectedItems.Count < 1)
        {
            MessageBox.Show("Please Select User");
        }
        else if (listView1.SelectedItems.Count > 1)
        {
            MessageBox.Show("Please Select Only One User");
        }
        else
        {
            _CurrentID = Convert.ToInt32(listView1.SelectedValue);
            GetPassword(_CurrentID);

            if (PasswordBox.Password == _CurrentPassword)
            {
                MessageBox.Show("You r in");
            }
            else
            {
                //MessageBox.Show("Password Is incorrect, please try again");
                MessageBox.Show(_CurrentPassword);
            }

        }

    }

enter image description here

0

4 Answers 4

4

You are missing a SingleOrDefault call or a call to Single, First or FirstOrDefault:

_CurrentPassword = (from p in db.users
                    where p.id == _CurrentID
                    select p.password).Single().ToString();

Explanation: Your query returns an IEnumerable<string> even if there is only one item returned. You first need to get that item, before you can make a sensible call to ToString(). BTW: I think the call to ToString() is not needed, because p.password already should be a string.

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

Comments

2

It looks like you're doing ToString() on the LINQ expression, not the password. Can you try removing .ToString() in GetPassword?

Also, as someone else pointed out, you need to select a single value from the enumerable -- SingleOrDefault() should do the trick.

Comments

1

The result of the expression:

from p in db.users
  where p.id == _CurrentID
  select p.password

is IQueryable<string>, not string (assuming p.password is a string). Use .First() or other method to just select a single element from the queryable collection.

Comments

1

What you are doing is to convert what is inside the linq query to a string, not the result of the query.You can

try

_CurrentPassword = (from p in db.users 
                    where p.id == _CurrentID 
                    select p.password).FirstOrDefault().ToString();

or

_CurrentPassword = (from p in db.users 
                    where p.id == _CurrentID 
                    select p.password).SingleOrDefault().ToString();

Please refer to these questions and answers to choose between Single(), SingleOrDefault(), First(), FirstOrDefault().

When to use .First and when to use .FirstOrDefault with LINQ?

and

LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.