0

For this simple User class

public partial class Users
{
        public long Id { get; set; }
        public string User { get; set; }
        public string Password { get; set; }        
}

I have the WCF form to add new user working fine and checking to not allow duplicated User name

Now I have a WCF form for the logging in, 2 texbox (textBoxUser and password) and a button with this code:

    private void buttonOK_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("I want to validate the user and password");

        using (UserDBEntities db = new UserDBEntities())
        {
            if (db.Users.Any(o => o.User == textBoxUser.Text))
            {
                MessageBox.Show("The user " + textBoxUser.Text + " exist! Now I need to check if the password is right");

                var userAccepted = db.Users.Find(textBoxUser.Text);
            }
            else
                MessageBox.Show("User or password wrong. Try again!");
        }

But the line

var userAccepted = db.Users.Find(textBoxUser.Text);

is not working - I keep getting an error:

ArgumentException was unhandled

An unhandled exception of type 'System.ArgumentException' occurred in EntityFramework.dll

Additional information: The type of one of the primary key values did not match the type defined in the entity. See inner exception for details.

even if User and textBoxUser.Text are strings.

And I don't know how to load the object from the database so I can check if the password is OK.

3
  • 1
    db.Users.First(u => u.User == textBoxUser.Text) will return the object the same way db.Users.Any(u => u.User == textBoxUser.Text) will return if it exists Commented Feb 14, 2018 at 14:30
  • @Rafalon Right I already get the "MessageBox.Show("The user " + textBoxUser.Text + " exist! Now I need to check if the password is right"); " working but how can I show the field Password from that object? Commented Feb 14, 2018 at 14:35
  • 1
    Check my answer for more info :) Commented Feb 14, 2018 at 14:44

1 Answer 1

1

You need to use Enumerable.First instead of List<T>.Find

if (db.Users.Any(o => o.User == textBoxUser.Text))
{
    MessageBox.Show("The user " + textBoxUser.Text
                  + " exists! Now I need to check if the password is right");

    User userAccepted = db.Users.First(o => o.User == textBoxUser.Text);

    MessageBox.Show(userAccepted.Password);
}

I would recommand changing your Users definition to:

public partial class User // note it's User and not Users
{
    public long Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }        
}

Then the answer would become:

if (db.Users.Any(o => o.Username == textBoxUser.Text))
{
    MessageBox.Show("The user " + textBoxUser.Text
                  + " exists! Now I need to check if the password is right");

    User userAccepted = db.Users.First(o => o.Username == textBoxUser.Text);

    MessageBox.Show(userAccepted.Password);
}

So you avoid confusion between the User class and its Username field. (you could as well just name it Name)

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

1 Comment

Thanks a lot. Is working now. I'm from a Spanish-speaking country and in Visual Studio we do not have the option to handle singular and plural, and of course I make a mistake on the transalation of the lesson. I mean, I don´t have the "Pluralize or singularize generated object names" while creating the Entity Model

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.