1
Answer

How to create user login in C# to login to MySql server database

In a test project I have successfully connected to mysql server database using Data Source connection string. I want to create login user accounts so that each user should login with credentials. I have created a login table on the server with each detailed accounts information, but am not successful when I start the project in the Visual Studio. Rather the following error message displays:

 

"Authentication to host 'localhost' for user 'root' using method 'caching_sha2_password' failed with message: Access denied for user 'root'@'localhost' (using password: NO)"

 

The following  is the code I used to create the login form in C# project. Please I would need help.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MySQLProject
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        MySqlConnection con = new MySqlConnection();
        MySqlCommand cmd = new MySqlCommand();
        MySqlDataAdapter da = new MySqlDataAdapter();
        DataTable dt = new DataTable();

        private void Login_Load(object sender, EventArgs e)
        {
            
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                con = new MySqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
                con.Open();

                cmd = new MySqlCommand("Select * from Login where username = '" + passwordTextBox.Text.ToString() + "' and password = '" + usernameTextBox.Text.ToString() + "'", con);
                da = new MySqlDataAdapter(cmd);
                dt = new DataTable();
                da.Fill(dt);

                passwordTextBox.Clear();
                usernameTextBox.Clear();
                this.Hide();

                string username = passwordTextBox.Text;
                string password = usernameTextBox.Text;

                if (dt.Rows.Count <= 0)
                {

                    MessageBox.Show("You Do Not Have Valid Access To Log in; Try Again Later", "Invalid Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Application.Exit();
                    Console.Beep();
                }

                else
                {
                    MessageBox.Show(username + "; You Are Successfully Logged In", "Valid Access", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                DBA dba = new DBA();
                User user = new User();

                cmd = new MySqlCommand("Select role from Login where username = @username and password = @password", con);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                string role = cmd.ExecuteScalar().ToString();
                MessageBox.Show("You Are Welcomed;  " + role, "Direction To User Home Page", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (role == "DBA")
                {
                    dba.Show();
                }
                else if (role == "User")
                {
                    user.Show();
                }
                Console.Beep();

                con.Close();
                da.Dispose();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {

            DialogResult response;
            response = MessageBox.Show("Are You Sure You Want To Exit The Database?", "Login Option", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (response == DialogResult.OK)
            {
                Application.Exit();
                Console.Beep();
            }
            else
            {
                return;
            }
        }
    }
}

Answers (1)