Comparing textbox.text value to value in SQL Server

Ok so I am trying to compare the password and username of the login login using a custom validator using linq to get information from the database which always returns false, although on the validator someone can tell me where my below code is going wrong. It would be much appreciated ... thank you in advanced ...

protected void LoginValidate(object source, ServerValidateEventArgs args)
{
    TiamoDataContext context = new TiamoDataContext();

    var UsernameCheck = from User in context.Users
                      where User.Username == TextBoxLoginUsername.Text && User.Password == TextBoxLogInPassword.Text
                      select User.Username;

    var PasswordCheck = from User in context.Users
                    where User.Username == TextBoxLoginUsername.Text && User.Password == TextBoxLogInPassword.Text
                    select User.Password;

    String test1 = PasswordCheck.ToString();
    String test2 = UsernameCheck.ToString();

    if (test1 == TextBoxLogInPassword.Text && test2 == TextBoxLoginUsername.Text)
    {
        args.IsValid = true;
        Session["Username"] = TextBoxLoginUsername;
        Response.Redirect("UserProfile.aspx");

    }
    else 
    {
        args.IsValid = false;
    }

}

      

I don't know where I am going wrong. I know him, most likely some kind of stupid mistake, and I'm inexperienced in this ...

+2


a source to share


1 answer


UsernameCheck and PasswordCheck will collect and not separate values. Therefore, the ToString () method does not return what you think.

It probably returns "IEnumerable<string>"

You need to force your LINQ queries to return a single result and not a collection.



var user = context.Users.Where(u => u.Username==TextBoxLoginUsername.Text && 
                                    u.Password == TextBoxLogInPassword.Text)
               .FirstOrDefault();

string userNameCheck = user.Username;
string passwordCheck = user.Password;

      

As a side note, so using the keyword is a var

bit dangerous if you're not entirely sure about the return type of the statement you are writing. If you change your var

to string

s, the compiler will catch the type mismatch at compile time.

+2


a source







All Articles