Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to authenticate login control with sql database.But its not working.What am i missing.

Here is my code
C#
private bool Userlogin(string un, string pw)
{
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd=new SqlCommand("Select *from UserData where Username= @un and password=@pw",con );
    cmd.Parameters.AddWithValue("@un",un);
    cmd.Parameters.AddWithValue("@pw",pw);
    con.Open();
    string result=Convert.ToString(cmd.ExecuteScalar());
    if (string.IsNullOrEmpty(result)) return false;return true;
}
protected void login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    string un = login1.UserName;
    string pw = login1.Password;
    bool result = Userlogin(un, pw);
    if (result)
    {
        e.Authenticated = true;
        Session["username"] = un;
    }
    else
    {
        e.Authenticated = false;
    }
}
Posted
Updated 11-Jun-14 5:34am
v2
Comments
[no name] 11-Jun-14 11:29am    
You are missing a description of what "not working" means.
Debug and see where is the exact issue.

1 solution

try below
C#
protected void login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    string un = login1.UserName;
    string pw = login1.Password;
    bool result = Userlogin(un, pw);
    if (result)
    {
        FormsAuthentication.SetAuthCookie(un, true);
        e.Authenticated = true;
        Session["username"] = un;
    }
    else
    {
        e.Authenticated = false;
    }
}
 
Share this answer
 
Comments
Member 10878414 12-Jun-14 0:33am    
It is showing Invalid column name 'password' error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900