How to Create a Login System in C# and MS Access Database
Submitted by janobe on Monday, July 1, 2019 - 21:24.
In this tutorial, I will teach you how to create a simple login system using c# and ms access database. This method is a good start for you when you are dealing with MS Access database and c#. This is very easy to understand most especially by beginners in programming. All you have to do is follow the instructions that are shown below.
The complete source code is included you can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.Step 2
Do the form just like shown below.Step 3
Open the code editor by pressing the F7 on your keyboard. In the code editor, add a namespace to accessOleDB
libraries
- using System.Data.OleDb;
Step 4
Create a connection between C# and MS Access database. After that, declare all the classes and a string variable that is needed.- OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application .StartupPath + "\\logindb.accdb");
- OleDbCommand cmd;
- OleDbDataAdapter da;
- DataTable dt;
- string sql;
Step 5
Create a function for retrieving the total number of rows in the database.- private int login(string sql)
- {
- int maxrow = 0;
- try
- {
- con.Open();
- cmd.Connection = con;
- cmd.CommandText = sql;
- da.SelectCommand = cmd;
- da.Fill(dt);
- maxrow = dt.Rows.Count;
- }
- catch(Exception ex)
- {
- MessageBox.Show ( ex.Message);
- }
- finally
- {
- da.Dispose();
- con.Close();
- }
- return maxrow;
- }
Step 6
Write the following code for the login process.- private void button1_Click(object sender, EventArgs e)
- {
- int maxrow = 0;
- sql = "Select * From tbluser WHERE u_name = '" + textBox1.Text + "' AND u_pass='" + textBox2.Text + "'";
- maxrow = login(sql);
- if(maxrow > 0)
- {
- MessageBox.Show("Welcome User");
- groupBox1.Enabled = false;
- label3.Text = "Welcome " + dt.Rows[0].Field<string>("fullname") + " | Logout" ;
- textBox1.Clear();
- textBox2.Clear();
- }
- else
- {
- MessageBox.Show("Your username and Password is incorrect.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
Step 7
Write the following code in the logout process.- private void label3_Click(object sender, EventArgs e)
- {
- groupBox1.Enabled = true;
- label3.Text = "Login System";
- }
Add new comment
- 3245 views