Advance Login System in C# and MySQL Database

In this tutorial, I will teach you how to create an Advance Login System in C# and MySQL database. I made this method to help you organize and minimize the codes that you have written in the system. It is composed of a function that you can call it anytime you want and put it in the event handler. In this way, it will simplify your work and it can be done in no time. To start, just follow the instructions below.

Creating Database

Create a database named “db_user”. Execute the following query for creating table and adding data in the table.
  1. CREATE TABLE `tbl_user` (
  2.   `UserID` int(11) NOT NULL,
  3.   `Fullname` varchar(30) NOT NULL,
  4.   `Username` varchar(90) NOT NULL,
  5.   `Pass` varchar(90) NOT NULL,
  6.   `UserType` varchar(30) NOT NULL
  7.  
  8. --
  9. -- Dumping data for table `tbl_user`
  10. --
  11.  
  12. INSERT INTO `tbl_user` (`UserID`, `Fullname`, `Username`, `Pass`, `UserType`) VALUES
  13. (1, 'Janno Palacios', 'janobe', 'd033e22ae348aeb5660fc2140aec35850c4da997', 'Administrator'),
  14. (2, 'Jeanniebe Nillos', 'jean', '6ccb4b7c39a6e77f76ecfa935a855c6c46ad5611', 'Staff');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#. psLoginSystem

Step 2

Do the Form just like this. psLoginSystemForm

Step 3

Add a namespace to access MySQL libraries.
  1. using MySql.Data.MySqlClient;

Step 4

Create a connection between C# and MySQL database and declare all the classes and variables that are needed.
  1.         MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=db_user;sslMode=none");
  2.         MySqlCommand cmd;
  3.         MySqlDataAdapter da;
  4.         DataTable dt;
  5.         string sql;
  6.         int maxrow;

Step 5

Create a method for the login.
  1.         private void doLogIn()
  2.         {
  3.             try
  4.             {
  5.                 sql = "SELECT * FROM `tbl_user` WHERE `Username`='" + txt_username .Text  + "'  AND `Pass`='"+ txt_password.Text  + "'";
  6.                 con.Open();
  7.                 cmd = new MySqlCommand();
  8.                 cmd.Connection = con;
  9.                 cmd.CommandText = sql;
  10.                 da = new MySqlDataAdapter();
  11.                 da.SelectCommand = cmd;
  12.                 dt = new DataTable();
  13.                 da.Fill(dt);
  14.              
  15.  
  16.                 maxrow = dt.Rows.Count;
  17.                 if (maxrow > 0)
  18.                 {
  19.                     MessageBox.Show("Welcome " + dt.Rows[0].Field<string>("UserType") );
  20.                     lbl_title.Text = "Hello, " + dt.Rows[0].Field<string>("Fullname");
  21.                     btn_logout.Visible = true;
  22.                     visibleAll(false);
  23.  
  24.                 }
  25.                 else
  26.                 {
  27.                     MessageBox.Show("Account does not exist.");
  28.                     txt_username.Focus();
  29.                 }
  30.  
  31.             }
  32.             catch (Exception ex)
  33.             {
  34.                 MessageBox.Show(ex.Message);
  35.             }
  36.             finally
  37.             {
  38.                 con.Close();
  39.                 da.Dispose();
  40.                    
  41.             }
  42.         }

Step 6

Create a method for hiding or showing the button,label and picture box.
  1.         private void visibleAll(Boolean result)
  2.         {
  3.             btn_close.Visible = result;
  4.             btn_login.Visible = result;
  5.             txt_password.Visible = result;
  6.             txt_username.Visible = result;
  7.             label2.Visible = result;
  8.             label3.Visible = result;
  9.             pictureBox1.Visible = result;
  10.             txt_password.Clear();
  11.             txt_username.Clear();
  12.             txt_username.Focus();
  13.         }

Step 7

Create a method for the Logout.
  1.         private void doLogOut()
  2.         {
  3.             visibleAll(true);
  4.             btn_logout.Visible = false;
  5.             lbl_title.Text = "Login System";
  6.         }

Step 8

Do the following codes for hiding the “Logout” button in the first load of the form.
  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             btn_logout.Visible = false;
  4.             this.AcceptButton = btn_login;
  5.         }

Step 9

Write the following codes to execute the login method when the button is clicked.
  1.         private void btn_login_Click(object sender, EventArgs e)
  2.         {
  3.             doLogIn();
  4.         }

Ste 10

These codes are for the “Logout” button.
  1.         private void btn_logout_Click(object sender, EventArgs e)
  2.         {
  3.             doLogOut();
  4.         }

Step 11

These codes are for the “Close” button.
  1.         private void btn_close_Click(object sender, EventArgs e)
  2.         {
  3.             this.Close();
  4.  
  5.         }

Comments

good source code it helps than coding new scratch

new scratch takes time to code than the ready source code

Visit the site everyday for more updates in programming.

Add new comment