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.
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
Do the form just like shown below.
Open the code editor by pressing the F7 on your keyboard. In the code editor, add a namespace to access
- using System.Data.OleDb;
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;
Create a function for retrieving the total number of rows in the database.
- private int login(string sql)
- int maxrow = 0;
- cmd = new OleDbCommand();
- da = new OleDbDataAdapter();
- dt = new DataTable();
- cmd.Connection = con;
- cmd.CommandText = sql;
- da.SelectCommand = cmd;
- maxrow = dt.Rows.Count;
- catch(Exception ex)
- MessageBox.Show ( ex.Message);
- return maxrow;
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.Field<string>("fullname") + " | Logout" ;
- MessageBox.Show("Your username and Password is incorrect.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
Write the following code in the logout process.
- private void label3_Click(object sender, EventArgs e)
- groupBox1.Enabled = true;
- label3.Text = "Login System";
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.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.