Advance Login System in C# and MySQL Database
Submitted by janobe on Friday, December 7, 2018 - 21:32.
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.- --
- -- Dumping data for table `tbl_user`
- --
- (1, 'Janno Palacios', 'janobe', 'd033e22ae348aeb5660fc2140aec35850c4da997', 'Administrator'),
- (2, 'Jeanniebe Nillos', 'jean', '6ccb4b7c39a6e77f76ecfa935a855c6c46ad5611', 'Staff');
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application in c#.Step 2
Do the Form just like this.Step 3
Add a namespace to access MySQL libraries.- using MySql.Data.MySqlClient;
Step 4
Create a connection between C# and MySQL database and declare all the classes and variables that are needed.- MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=db_user;sslMode=none");
- MySqlCommand cmd;
- MySqlDataAdapter da;
- DataTable dt;
- string sql;
- int maxrow;
Step 5
Create a method for the login.- private void doLogIn()
- {
- try
- {
- sql = "SELECT * FROM `tbl_user` WHERE `Username`='" + txt_username .Text + "' AND `Pass`='"+ txt_password.Text + "'";
- con.Open();
- cmd.Connection = con;
- cmd.CommandText = sql;
- da.SelectCommand = cmd;
- da.Fill(dt);
- maxrow = dt.Rows.Count;
- if (maxrow > 0)
- {
- MessageBox.Show("Welcome " + dt.Rows[0].Field<string>("UserType") );
- lbl_title.Text = "Hello, " + dt.Rows[0].Field<string>("Fullname");
- btn_logout.Visible = true;
- visibleAll(false);
- }
- else
- {
- MessageBox.Show("Account does not exist.");
- txt_username.Focus();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();
- da.Dispose();
- }
- }
Step 6
Create a method for hiding or showing the button,label and picture box.- private void visibleAll(Boolean result)
- {
- btn_close.Visible = result;
- btn_login.Visible = result;
- txt_password.Visible = result;
- txt_username.Visible = result;
- label2.Visible = result;
- label3.Visible = result;
- pictureBox1.Visible = result;
- txt_password.Clear();
- txt_username.Clear();
- txt_username.Focus();
- }
Step 7
Create a method for the Logout.- private void doLogOut()
- {
- visibleAll(true);
- btn_logout.Visible = false;
- lbl_title.Text = "Login System";
- }
Step 8
Do the following codes for hiding the “Logout” button in the first load of the form.- private void Form1_Load(object sender, EventArgs e)
- {
- btn_logout.Visible = false;
- this.AcceptButton = btn_login;
- }
Step 9
Write the following codes to execute the login method when the button is clicked.- private void btn_login_Click(object sender, EventArgs e)
- {
- doLogIn();
- }
Ste 10
These codes are for the “Logout” button.- private void btn_logout_Click(object sender, EventArgs e)
- {
- doLogOut();
- }
Step 11
These codes are for the “Close” button.- private void btn_close_Click(object sender, EventArgs e)
- {
- this.Close();
- }
Comments
Add new comment
- Add new comment
- 867 views