How to Create a Login System in C# and MS Access Database

Submitted by: 
Language: 
Visitors have accessed this post 375 times.

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.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
ps1

Step 2

Do the form just like shown below.
ps2

Step 3

Open the code editor by pressing the F7 on your keyboard. In the code editor, add a namespace to access OleDB libraries

  1.  
  2. 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.

  1.  
  2. OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application .StartupPath + "\\logindb.accdb");
  3. OleDbCommand cmd;
  4. OleDbDataAdapter da;
  5. DataTable dt;
  6. string sql;

Step 5

Create a function for retrieving the total number of rows in the database.

  1.  
  2. private int login(string sql)
  3. {
  4. int maxrow = 0;
  5. try
  6. {
  7. con.Open();
  8. cmd = new OleDbCommand();
  9. da = new OleDbDataAdapter();
  10. dt = new DataTable();
  11.  
  12. cmd.Connection = con;
  13. cmd.CommandText = sql;
  14.  
  15. da.SelectCommand = cmd;
  16. da.Fill(dt);
  17.  
  18. maxrow = dt.Rows.Count;
  19. }
  20. catch(Exception ex)
  21. {
  22. MessageBox.Show ( ex.Message);
  23. }
  24. finally
  25. {
  26. da.Dispose();
  27. con.Close();
  28. }
  29. return maxrow;
  30. }

Step 6

Write the following code for the login process.

  1.  
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. int maxrow = 0;
  5. sql = "Select * From tbluser WHERE u_name = '" + textBox1.Text + "' AND u_pass='" + textBox2.Text + "'";
  6. maxrow = login(sql);
  7.  
  8. if(maxrow > 0)
  9. {
  10. MessageBox.Show("Welcome User");
  11. groupBox1.Enabled = false;
  12. label3.Text = "Welcome " + dt.Rows[0].Field<string>("fullname") + " | Logout" ;
  13.  
  14. textBox1.Clear();
  15. textBox2.Clear();
  16. }
  17. else
  18. {
  19. MessageBox.Show("Your username and Password is incorrect.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
  20. }
  21. }

Step 7

Write the following code in the logout process.

  1.  
  2. private void label3_Click(object sender, EventArgs e)
  3. {
  4. groupBox1.Enabled = true;
  5. label3.Text = "Login System";
  6. }

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.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.