Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
When i login with incorrect username and password then it redirect to webform2.aspx where i want to show message Wrong username and password

how i do that ?

i am trying below code but it is not working

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        if(loginmethod(txt_us.Text,txt_pwd.Text)!="NA")
        {
            FormsAuthentication.Initialize();
            String strRole =Assignroles(txt_us.Text);
            FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, txt_us.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, strRole, FormsAuthentication.FormsCookiePath);
            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
            FormsAuthentication.Encrypt(fat)));
            loginmethod(txt_us.Text, txt_pwd.Text);
            Response.Redirect("WebForm2.aspx");
        }
        else
        {
            Label1.Text = ("Incorrect UserName/Password");
            Label1.Visible = true;
        }
        txt_us.Text = "";
        txt_pwd.Text = "";
    }
    private string loginmethod(string UserName, string Password)
    {
       try
       {
           login_class lg_class = new login_class();
           Entities2 login = new Entities2();
           string logn = Convert.ToString(lg_class.loginfu(UserName, Password).Rows[0]["id"]);
           Session["ID"] = logn.ToString();
           return (logn);
       }
       catch (Exception ex)
       {

           return (ex.Message.ToString());

       }

    }


What I have tried:

loginfu method

public DataTable loginfunction(string username,string password)
{
try
{
Entities2 lg = new Entities2();
List<sp_getlogin_result> gr = lg.SP_GetLogin(username, password).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
foreach (var l in gr)
{
dt.Rows.Add(l.id);
}
return dt;
}

catch (Exception)
{
throw new Exception();

}

}
Posted
Comments
Karthik_Mahalingam 5-Sep-16 4:36am    
debug and check the value in dt
super_user 5-Sep-16 7:04am    
dt returns id .. this return id against username and password
Karthik_Mahalingam 5-Sep-16 7:12am    
okas per your code, if the id is not 'na' it will validate and redirect to nextpage (WebForm2)
else it will display the error message in the label.
super_user 6-Sep-16 8:02am    
yes @Karthik Bangalore
Karthik_Mahalingam 6-Sep-16 10:16am    
then! what is the issue

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