简体   繁体   中英

ASP.net Entity Framework Check if exists in database

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?

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM