C# - How To Create Login Form In C# With MySQL Database

C# - How To Create Login Window In C# With MySQL Database

                                                                                                                                         

In This C# Tutorial We Will See How To Make A Login Form Using CSharp Programming Language And MySQL Database.

                                                      
                                                                      Part 1


Part 2

Source Code :

using MySql.Data.MySqlClient;
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;

namespace Csharp_And_MySQL
{
    public partial class Login_Form_2 : Form
    {
        public Login_Form_2()
        {
            InitializeComponent();
        }

        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='test';username=root;password=");

        MySqlDataAdapter adapter;

        DataTable table = new DataTable();

        private void BTN_Login_Click(object sender, EventArgs e)
        {
            adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + textBox_username.Text + "' AND `password` = '" + textBox_password.Text + "'", connection);
            adapter.Fill(table);

            if(table.Rows.Count <= 0)
            {
                panel1.Height = 0;
                label_Message.ForeColor = Color.Red;
                label_Message.Text = "Username Or Password Are Invalid";
                timer1.Start();
            }
            else
            {
                panel1.Height = 0;
                label_Message.ForeColor = Color.Green;
                label_Message.Text = "Login Successfully";
                timer1.Start();
            }

            table.Clear();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(panel1.Height != 100)
            {
                panel1.Height = panel1.Height + 5;
                if(panel1.Height == 100)
                {
                    timer1.Stop();
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (panel1.Height != 0)
            {
                panel1.Height = panel1.Height - 5;
                if (panel1.Height == 0)
                {
                    timer2.Stop();
                }
            }
        }

        private void BTN_OK_Click(object sender, EventArgs e)
        {
            timer2.Start();
        }

        private void checkBox_ShowPass_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_ShowPass.Checked)
            {
                textBox_password.UseSystemPasswordChar = true;
            }
            else
            {
                textBox_password.UseSystemPasswordChar = false;
            }
        }
       
    }
}

=> OutPut :

c# login window
Login Failed

c# login window
 Login Successfully




Share this

Related Posts

Previous
Next Post »

5 comments

comments
8 novembre 2015 à 23:04 delete

Hello you could help me in my project? http://prntscr.com/90md0t

Reply
avatar
8 novembre 2015 à 23:10 delete

try adapter.Fill(table);not fill
and add the connection name to the MySqlDataAdapter Constructor adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '"+textBox_Username.Text+"' AND `password` = '"+textBox_Password.Text+"'", connection);

Reply
avatar
11 juin 2017 à 03:53 delete

using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace LoginForm_PhpMyAdmin
{
public partial class Form1 : Form
{
public Form1()
{
MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='visual';username=root;password=root");
con.Open();
InitializeComponent();
}



private void btnlgn_Click(object sender, EventArgs e)
{

MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='visual';username=root;password=root");
con.Open();
string query = "SELECT * FROM `login` WHERE `Username` = '" + textuser.Text + "' and `Password` = '" + textpswd.Text + "'";

MySqlCommand myCommand = new MySqlCommand(query,con);
MySqlDataAdapter MyAdapter = new MySqlDataAdapter(myCommand);

DataTable dt = new DataTable();

MyAdapter.Fill(dt);

if (dt.Rows.Count > 0)
{
MessageBox.Show("Login Successful.");
}
else
{
MessageBox.Show("Please Enter Valid Username Or Password!");
}
}
}
}

Reply
avatar
11 juin 2017 à 03:54 delete

Can anyone help me? I am unable to login

Reply
avatar
11 mai 2018 à 15:28 delete

hi bro .. i need your help about this login please

Reply
avatar