How to Login User Accounts from Microsoft Access Database using C#

This tutorial is a continuation of our last topic called “Searching a Record From Database using C#”. But this time,we will be focusing on how to create an application that will allow the user to create a login system where the user accounts stored in the database.And now we will be using the Microsoft database we created from our last tutorial and we’re going to create a new project called “userlogin”. Then design the user interface that looks like as shown below. u1 Next, let’s start adding functionalities to our application. First double click the form and you will be redirected to code window. Then on the “namespace userlogin”, add this single line of code:
  1. using System.Data.OleDb;
The new added code is a .NET Framework Data Provider for OLE DB describes a collection of classes used to access an OLE DB data source in the managed space. And under the “public partial class Form1 : Form  {“ add the following code:
  1. //declare new variable named dt as New Datatable
  2. DataTable dt = new DataTable();
  3. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  4. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + "/usermgt.mdb";
  5. OleDbConnection conn = new OleDbConnection(connection);
  6. <csharp>
  7. The code above will simply declare a new variable the following will make the connection to the server an locate our database named “usermgt.mdb.
  8.  
  9. Next, double click the “<strong>Login</strong>” button and add the following code:
  10. <csharp>
  11. dt = new DataTable();
  12. //this query will give us the result based on user input for Username and Password
  13. string sql = "Select * from tbluseraccounts where userusername='"+ txtuser.Text +"' AND userpassword='"+ txtpass .Text +"'";
  14. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  15. da.Fill(dt);
  16. //then the result will get the name of the user and display it in the form using label control.
  17. label3.Text = dt.Rows[0][2].ToString();
At this time you can run and test the application.And as you observe, the application has a bug. The bug will occur when the user input something in the username and Password which is not found in the database. The error is look like as shown below: u2 To solve this bug, we’re going to modify our code. And this will now look like as shown below:
  1. dt = new DataTable();
  2. //this query will give us the result based on user input for Username and Password
  3. string sql = "Select * from tbluseraccounts where userusername='"+ txtuser.Text +"' AND userpassword='"+ txtpass .Text +"'";
  4. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  5. da.Fill(dt);
  6.  
  7. if (dt.Rows.Count > 0)
  8. {
  9. //then the result will get the name of the user and display it in the form using label control.
  10. label3.Text = dt.Rows[0][2].ToString();
  11. }
  12. else
  13. {
  14. MessageBox.Show("Username or Password Not registered!Please Contact administrator!");
  15. }
In our new modified code above, we simply first check if there’s a result found based on the user inputs. And if there’s a result, it will display the name of the user who logged in. And this will look like as shown below: u3 Else if there’s no result, it will show a message dialog box looks like as shown below: u4 Here’s all the code used in this application:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. using System.Data.OleDb;
  11.  
  12. namespace userlogin
  13. {
  14. public partial class Form1 : Form
  15. {
  16. //declare new variable named dt as New Datatable
  17. DataTable dt = new DataTable();
  18. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  19. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + "/usermgt.mdb";
  20. OleDbConnection conn = new OleDbConnection(connection);
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28.  
  29. }
  30.  
  31. private void btnlogin_Click(object sender, EventArgs e)
  32. {
  33. dt = new DataTable();
  34. //this query will give us the result based on user input for Username and Password
  35. string sql = "Select * from tbluseraccounts where userusername='"+ txtuser.Text +"' AND userpassword='"+ txtpass .Text +"'";
  36. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  37. da.Fill(dt);
  38.  
  39. if (dt.Rows.Count > 0)
  40. {
  41. //then the result will get the name of the user and display it in the form using label control.
  42. label3.Text = "Hi, " + dt.Rows[0][2].ToString();
  43. }
  44. else
  45. {
  46. MessageBox.Show("Username or Password Not registered!Please Contact administrator!");
  47. }
  48.  
  49. }
  50. }
  51. }

Add new comment