简体   繁体   中英

ASP.NET Entity Framework Label show Database entry

I am using Entity Framework 6, Visual Studio 2015 and a simple one table database ("Logins") that is connected via entity framework 6.

Database table only has the fields:

Firstname, lastname, birthdate, password, email

Right now, it can test if a username and password exist, HOWEVER, if it exists, I need it to display the First and Last name of that person in my two label fields

labelFirst and labelLast

I've tried a few things, but haven't been able to get it to work with a label. This is what I have when the login button is clicked:

protected void btnLogin_Click(object sender, EventArgs e)
{
    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)
        {
            lblSuccess.Visible = true;
            lblFail.Visible = false;

            // Clearly, this part doesn't work...

            labelFirst.DataSource = lg.Logins.ToList();
            labelFirst.DataBind();
            labelLast.DataSource = lg.Logins.ToList();
            labelLast.DataBind();

            lbFirstName.Visible = true;
            lbLastName.Visible = false;
        }
        else
        {
            lblFail.Visible = true;
            lblSuccess.Visible = false;
        }
    }
}

How do I go about having the email/pass that was valid show their name in the labels?

You have a label and you're trying to bind a List to that label. You shouldn't even be using databinding.

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

            labelFirst.Text = logged.FirstName;
            labelLast.Text = logged.LastName;

            lbFirstName.Visible = true;
            lbLastName.Visible = false;
        }

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