10/04/2012

How to create a login page? ASP.net for beginners



Creating a login page in ASP. Net is quite easy and simple. Follow the following steps for creating login page.

  • Create table loing in your database with fields UserName and Password.
  • Enter Few user name and password.
  • Add a new web form on existing project or create a new web application.
  • Add two label control, two text boxes for user name and password fields.
  • Add Ok and Cancel Buttons.
  • Add a label control and empty the text field..
  • Write the following line at the beginning of web form's code-behind page.
                    Imports system.Data.sqlclient
  • Double click on OK button and write the following line in the OK button clickeevent
                ' Validating the control
                lblmessage.text             = string.empty
                lblmessage.forecolor    = color.blue
                txtUsername.text         = txtusername.text.trim
               txtpassword.text          = txtPassword.text.trim

     if    txtUsername.text     =   ""   then
            lblMessage.text = "Enter User Name"
           exit sub
     end if
    if      txtUsername.text.length <  8  then
           lblmessage.text = "User name should be  at least 8 characters"
           Exit sub
     end if
     if    txtPassword.text   =   ""   then
         lblmessage.text " Enter the Password"
         exit sub
     end if
     if    txtPassword.text.length < 8  then
          lblMessage.text = " User Password should be at least 8 characters."
          exit sub
     end if

            Dim CnMyDB As New SqlConnection
            Dim CmdSelect As SqlCommand
            Dim DtLogin As SqlDataReader

            cnMyDb= New SqlConnection( " server= localhost;uid=sa;pwd=;database=MyDB")
            CmdSelect= New SqlCommand("select * from Login where UserName = '" & txtUsername.text & "'    
            and Password = '" & txtPassword.text & "'", cnMyDB)
          CnMyDB.open
           dtrLogin = cmdSelect.ExecuteReader

         if   dtrLogin.HasRows   then
             lblMessage.Forecolor =color.Green
             lblMessage.Text = " Welcome to My Website"
       else
             lblMessage.Forecolor = Color.Red
             lblMessage.Test = " Login Failed ! Try Again! "
       end if
      dtrLog.close
      CnMyDB.close

  • Write the following code for clear button' click event

           LblMessage.Text = lblMessage.Text.Empty
           txtUserName.Text = txtUserName.Text.Empty
           txtPassword.Text= txtPassword.Text.Empty
  • Save and run the project
 To Know more about such ASP.Net the best books are available on on flipKart and Amazon.These are some books which I will suggest every one who is starting web designing must buy.

Murach ASP.Net 3.5 Web Programming with Web Publisher 2008