Windows Forms Application Login Demo with MySQL Connection in C# .NET

After testing out Java for a couple of weeks, this weekend I decided to try out .NET. I am a big fan of web applications but recently I’ve taken an interest towards Java and Windows Forms Applications. So, the first thing that came to my attention was the Windows Application Forms in C#.

So, basically I’ve used Visual Studio 2012 and for database I’ve used EasyPHP Dev Server because MySQL is my most favorite database.

GUI Design

For GUI design, I kept it simple – some panels, labels and textboxes along with buttons.

Windows Forms Application

So, that cares of the simple GUI.

MySQL Database Connection

Before making the MySQL database connection, you need to download MySQL .NET Connector which you can download from here. After downloading the connector for the .NET and MONO platform, you need to extract it. Then you will need to add these two files – mysql.data.dll and mysql.web.dll as reference in your project. These two files are basically ADO .NET MySQL Driver.

Windows Forms Application

After the addition of these two references you can write the code. The most important function in Login is that of the “Submit” button. So, here’s the simple code that I used.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient; // required for MySQL connection after addition of MySQL References

namespace ApplicationDemo
{
    public partial class Form1 : Form
    {
        private string conn;
        private MySqlConnection connect;
        public Form1()
        {
            InitializeComponent();
        }

        private void db_connection()
        {
            try
            {
                conn = "Server=localhost;Database=netdemo;Uid=root;Pwd=;";
                connect = new MySqlConnection(conn);
                connect.Open();
            }
            catch (MySqlException e)
            {
                throw;
            }
        }

        private bool validate_login(string user, string pass)
        {
            db_connection();
            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = "Select * from login where username=@user and password=@pass";
            cmd.Parameters.AddWithValue("@user", user);
            cmd.Parameters.AddWithValue("@pass", pass);
            cmd.Connection = connect;
            MySqlDataReader login = cmd.ExecuteReader();
            if (login.Read())
            {
                connect.Close();
                return true;
            }
            else
            {
                connect.Close();
                return false;
            }
        }

        private void submit_Click(object sender, EventArgs e)
        {
            string user = username.Text;
            string pass = password.Text;
            if (user == "" || pass == "")
            {
                MessageBox.Show("Empty Fields Detected ! Please fill up all the fields");
                return;
            }
            bool r = validate_login(user, pass);
            if (r)
                MessageBox.Show("Correct Login Credentials");
            else
                MessageBox.Show("Incorrect Login Credentials");
        }
    }
}

So from above the name of the MySQL database is netdemo and the name of the table is login.

Validation and Output

For a little added security, I’ve added a validation parameter. If both or one of the textbox is empty while submitting the form, it will display a error message like the one below.

Windows Forms Application

For the correct username and password, it will show a message box of “Correct Login Credentials” and for an incorrect combo it will display a message box of “Incorrect Login Credentials”.

Windows Forms Application

So, that’s it for this one. Stay tuned for more Application Development Tutorials.

Share this post :
Lahaul Seth

Lahaul Seth

Software Engineer, Open Source Enthusiast & Blogger - I also own Lion Blogger Tech which focuses on Open Source, UI, UX, Technology.

I also own Open Source Software News which is a curation site for Open Source Technology News & Updates.

Also check out my personal site.