Create simple log In window form in C#

The following article describes how to create a simple user Log-in User Interface for the C# windows application.

1. Start a new Project in c#.

2. Add a new windows form in your project with name login.cs.

3. Re size the Login form and add two textbox named txtUserName and txtPassword,two label controls and one button control named btnOk.

4. set the PasswordChar property to astrick(*) of the txtPassword textbox.

Log in Window  title=

5. Write the function for validating the both textboxs:

  1.  private  bool  ValidateTextBoxes()
  2.         {
  3.             if (txtUserName.Text.Trim().Length == 0)
  4.             {              
  5.                 errorProvider1.SetError(txtUserName, "Please Enter User Name");
  6.                 return false;
  7.             }
  8.             else
  9.             {
  10.                 errorProvider1.SetError(txtUserName, "");
  11.             }
  12.             if (txtPassword.Text.Trim().Length == 0)
  13.             {
  14.                 errorProvider1.SetError(txtPassword, "Please Enter the Password");
  15.                 return false;
  16.             }
  17.             else
  18.             {
  19.                 errorProvider1.SetError(txtPassword, "");
  20.             }
  21.             return true;
  22.         }

6. Verify he correct user name and password from the SQL database:

  1.  private void btnOk_Click(object sender, EventArgs e)
  2.         {
  3.             if (ValidateTextBoxes())
  4.             {
  5.                 SqlConnection oConn = new SqlConnection();
  6.                 oConn.ConnectionString = "Write your Database connection string";
  7.                 oConn.Open();
  8.                 string strQuery = "select id from tblUser where loginName='"+ txtUserName.Text +"' and Password="+  txtPassword.Text +"";
  9.                 object retVal;
  10.                 SqlCommand cmd = new SqlCommand(strQuery, oConn);
  11.                 retVal = cmd.ExecuteScalar();
  12.                 if (retVal != null)
  13.                 {
  14.                     this.DialogResult = DialogResult.OK;
  15.                 }
  16.                 else
  17.                 {
  18.                     this.DialogResult = DialogResult.Cancel;
  19.                     //Authontication Failed
  20.                 }
  21.             }
  22.         }

7. Write the following code in program.cs:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4.  
  5. namespace WindowsApplication1
  6. {
  7.     static class Program
  8.     {
  9.         /// <summary>
  10.         /// The main entry point for the application.
  11.         /// </summary>
  12.         [STAThread]
  13.         static void Main()
  14.         {
  15.             Application.EnableVisualStyles();
  16.             Application.SetCompatibleTextRenderingDefault(false);
  17.             Application.Run(new frmlogin());
  18.             frmlogin objLogin = new frmlogin();
  19.             if (objLogin.ShowDialog() == DialogResult.OK)
  20.             {
  21.                 // successfully login
  22.                 //Do Somthing……
  23.             }
  24.             else
  25.             {
  26.             }
  27.         }
  28.     }
  29. }

8. Run the project.

facebooktwittergoogle_plusredditpinterestlinkedinmail
  • ElChristoph

    I believe you need to take out a line to make this work:

    //Application.Run(new frmlogin());

    Then use Applicatn.Run(new MyMainForm()); in the if statement.