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.
5. Write the function for validating the both textboxs:
private bool ValidateTextBoxes() { if (txtUserName.Text.Trim().Length == 0) { errorProvider1.SetError(txtUserName, "Please Enter User Name"); return false; } else { errorProvider1.SetError(txtUserName, ""); } if (txtPassword.Text.Trim().Length == 0) { errorProvider1.SetError(txtPassword, "Please Enter the Password"); return false; } else { errorProvider1.SetError(txtPassword, ""); } return true; }
6. Verify he correct user name and password from the SQL database:
private void btnOk_Click(object sender, EventArgs e) { if (ValidateTextBoxes()) { SqlConnection oConn = new SqlConnection(); oConn.ConnectionString = "Write your Database connection string"; oConn.Open(); string strQuery = "select id from tblUser where loginName='"+ txtUserName.Text +"' and Password="+ txtPassword.Text +""; object retVal; SqlCommand cmd = new SqlCommand(strQuery, oConn); retVal = cmd.ExecuteScalar(); if (retVal != null) { this.DialogResult = DialogResult.OK; } else { this.DialogResult = DialogResult.Cancel; //Authontication Failed } } }
7. Write the following code in program.cs:
using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsApplication1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmlogin()); frmlogin objLogin = new frmlogin(); if (objLogin.ShowDialog() == DialogResult.OK) { // successfully login //Do Somthing...... } else { } } } }
8. Run the project.
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.
Hellow Sir how are you.
your login for is too help but I wana create a re-set able passward form in Visual C# Winapp, Please help to create Login form with re-set passward coding…