This article deals with how to create a Windows Form Application in C# as a Login Page which basically all applications contain as the starting page, where users will be validated for their passwords.
Step 1
Open Visual Studio 2015 and click on File –> New –> Project which opens up the template window with many developing options.
Step 2
The above step brings in an empty Windows Form where the user can use Tools for designing the empty form into interactive form. I have used Label (2 nos), TextBox (2 nos) and a Button . Placing the tools in the right designing way, it looks like the below image.
Step 3
The conventional way of any login form will contain password displayed in a special character which is for security purposes. To bring that concept into your login form, select the Properties option of TextBox in which you could find the option Password Char where you can give your desired special character (in my case, I have used * asterisk). By making this change, if you run your application, it will display only the special symbol when you enter a password.
Step 4:
Now, it is time for creating the repository for your login form. For this, go to Start menu and find the MS Access and click once to open.
Step 5:
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace loginformcce { public partial class Form1 : Form { OleDbConnection con; OleDbCommand cmd; OleDbDataReader dr; public Form1() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { string usr = txtUser.Text; string psw = txtPass.Text; con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=dbUsers.accdb"); cmd = new OleDbCommand(); con.Open(); cmd.Connection = con; cmd.CommandText = "SELECT * FROM tblUser where user='" + txtUser.Text + "' AND pass='" + txtPass.Text + "'"; dr = cmd.ExecuteReader(); if (dr.Read()) { Form2 f2 = new Form2(); f2.Show(); } else { MessageBox.Show("Username or password is incorrect"); } con.Close(); } } } |
Output: