Pages

Search This Blog

Monday 12 May 2014

How to validate user in Login Page ??

Hi friends, Today I will say you “How to write code for login page”

For login we need mainly two textboxes to take username , password as input and one button in that we will write the code for checking in the database whether the entered username was valid or not. If not valid we will show an error message.

Here is the code for login page :

Login.aspx.cs

protected void login_Click(object sender, EventArgs e)
    {
      
        try
        {
      
      
        con.ConnectionString = str;
        con.Open();

        SqlDataReader dr;
// way 1 writing procedure
        SqlCommand cmd1 = new SqlCommand("sp_getlogin", con);

        cmd1.CommandType = CommandType.StoredProcedure;
  SqlParameter x1 = new SqlParameter("@user", tblogin.Text.Trim());
  SqlParameter x2 = new SqlParameter("@pwd", tbpwd.Text.Trim());

        cmd1.Parameters.Add(x1);
        cmd1.Parameters.Add(x2);

  // way 2  writing command here itself     
 SqlCommand cmd1 = new SqlCommand("select * from login where name=”’” +uname.Text+”’ and password=”’”+password.Text+”’", con);
   
    dr = cmd1.ExecuteReader();

     // It checks whether the username and password row is there in the
    //data base table or not
            if (!dr.HasRows)
            {

Response.Write("<script>alert('Invalid Username or Password!');</script>");

            }
            else
            {
        // If login details are correct it will redirect to “Display page”
      Response.Redirect("./Display.aspx");
                                
            }
        }
            catch(Exception)
            {
              Response.Write("<script> alert('Invalid Login details !')</script>");
            }
       
        con.Close();
    }

You can rate my ASP.NET class on right side top....  

                                                                 -Your's Santoshklpkl

No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget