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

Irakli Lekishvili
  • 33,492
  • 33
  • 111
  • 169

4 Answers4

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.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
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.

Bastardo
  • 4,144
  • 9
  • 41
  • 60
JohnD
  • 14,327
  • 4
  • 40
  • 53
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.

Richard
  • 106,783
  • 21
  • 203
  • 265
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

Community
  • 1
  • 1
Bastardo
  • 4,144
  • 9
  • 41
  • 60