1

I have VS2015, entity framework 6. I have a database with one table (Logins)

FirstName, lastName, Birthdate, email, password

I also have a textbox(s), button

tbEmail tbpass and btnLogin

How do I check if the users email in the textbox matches one in the database?

So far I have:

 protected void btnLogin_Click(object sender, EventArgs e)
    {
            Logins Log = new Logins();

    using (LoginDataEntities lg = new LoginDataEntities())
    {
        string @email = tbUsernameL.Text;
        string @password = tbPassL.Text;

        var logged = from L in lg.Logins
                     where L.Username == @email
                     && L.Pass == @password
                     select L.Username;



        if (logged != null) 
        {
            lblSuccess.Visible = true;
        }
        else
        {
            lblFail.Visible = true;
        }


    }
}

However, its not working and always enables the success label. How do I fix this?

1 Answer 1

1

Try it once with the following snippet:

using (LoginDataEntities lg = new LoginDataEntities())
        {
            string @email = tbUsernameL.Text;
            string @password = tbPassL.Text;

            var logged = lg.Logins
.SingleOrDefault(l=> l.Username == @email && l.Pass == @password);

            if (logged != null) // update
            {
                lblSuccess.Visible = true;
            }
            else
            {
                lblFail.Visible = true;
            }
        }

Alternatively, can you also look at the following example again:

http://www.c-sharpcorner.com/uploadfile/b19d5a/custom-user-login-and-registration-page-in-Asp-Net-mvc3-with-razor-and-entity-framework/

Or you refactorisiers the VS template with Individual User Accounts

enter image description here

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

2 Comments

I am getting an error with SingleorDefault -- "Error CS1061 LoginDataEntities' does not contain a definition for 'SingleOrDefault' and no extension method 'SingleOrDefault' accepting a first argument of type 'LoginDataEntities' could be found"
Ops sry, lg.Logins.SingleOrDefault(l=> l.Username == @email && l.Pass == @password); i misst your entity, i will correct in my snippet..

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.