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.
For GUI design, I kept it simple – some panels, labels and textboxes along with buttons.
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.
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 MySql.Data.MySqlClient; // required for MySQL connection after addition of MySQL References
public partial class Form1 : Form
private string conn;
private MySqlConnection connect;
private void db_connection()
conn = "Server=localhost;Database=netdemo;Uid=root;Pwd=;";
connect = new MySqlConnection(conn);
catch (MySqlException e)
private bool validate_login(string user, string pass)
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connect;
MySqlDataReader login = cmd.ExecuteReader();
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");
bool r = validate_login(user, pass);
MessageBox.Show("Correct Login Credentials");
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.
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”.
So, that’s it for this one. Stay tuned for more Application Development Tutorials.