Search code examples
nhibernatenhibernate-criteria

Using Nhibernate I want to use username and password to login


I like the people to help me regarding that the username and password details are present in the table TUser .My task is how can i check the when i login using username:admin and password:admin and press login button it searches with database and then i can login inside using Nhibernate.Please help me .this is the code below

ISession session = NHibernateHttpModule.CurrentSession;
protected void rdlogin_Click1(object sender, EventArgs e)
{
    TUsers users = session.CreateCriteria(typeof(TUsers))
        .Add(Expression.Eq(txtusername.text, "UserID"))
        .Add(Expression.Eq(txtpassword.text, "Password"))
        .UniqueResult<TUsers>();

    if (users != null)
    {
        Response.Redirect("Default.aspx");
    }
    else
    {
        Label1.Visible=true;
        Label1.Text = "Invalid user name and passwprd";
    }
}

Solution

  • You need to change a few things:

    1. You need to be absolutely sure that there is only one unique result for your query. More than one result will throw an exception.
    2. You need to switch the property and values in your Expressions.
    3. You need to trim your TextBox text fields.

       TUsers users = session.CreateCriteria(typeof(TUsers))
           .Add(Expression.Eq("UserID",txtusername.text.Trim()))
           .Add(Expression.Eq("Password",txtpassword.text.Trim()))
           .UniqueResult<TUsers>();