Click here to Skip to main content
15,888,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a web application. Whenever any user tries to login into that application using wrong credentials, it should show alert" Please enter correct Userid and Password". But the problem is that, the page keeps on loading in the backend until the user does not click the "ok" button of the message. I want to avoid page loading after error message has been displayed. plz help.

What I have tried:

Below are my code:

HTML:
                                <asp:Button ID="btnLogin" runat="server" Text="Login" class="button" OnClientClick="return validateTextPw()" OnClick="btnLogin_Click" formmethod="post" />


C#:

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MmrtgDBConnString"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from MSTR_USER where UserId =@username and UserPassword=@password", con);
            cmd.Parameters.AddWithValue("@username", txtUserId.Value);
            cmd.Parameters.AddWithValue("@password", txtPsd.Value);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
               ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
            }
        }
Posted
Updated 24-May-19 0:10am
Comments
Richard Deeming 24-May-19 14:08pm    
You're storing passwords in plain text. Don't do that.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

A better option would be to use one of the perfectly good built-in authentication systems, which would take care of this sort of thing for you. For example: ASP.NET Identity[^]

1 solution

After displaying the error message, redirect back to the login page.
 
Share this answer
 

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