<repo_name>Wea1Storm/Login-App<file_sep>/Login App/MainWindow.cs
﻿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 Login_App
{
    public partial class MainWindow : Form
    {     
        public MainWindow()
        {

            InitializeComponent();
        }

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

        private void MainWindow_Load(object sender, EventArgs e)
        {
            UserNameLabel.Text = User.UserName;
            RootLable.Text = User.Root;
        }

        private void UserNameLabel_Click(object sender, EventArgs e)
        {

        }
    }
}
<file_sep>/Login App/Registration.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace Login_App
{
    public partial class Registration : Form
    {
        string connectionString = ConfigurationManager.ConnectionStrings["UsersConnection"].ConnectionString;

        public Registration()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            LoginForm lf = new LoginForm();
            this.Hide();
            lf.Show();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (isValid() && isLoginExist())
            {
                using(SqlConnection db = new SqlConnection(connectionString))
                {
                    string query = $"INSERT INTO LoginTable (UserName, Password) values('{txtName.Text.TrimStart()}', '{txtPassword.Text.TrimStart()}')";

                    db.Open();

                    SqlDataAdapter sda = new SqlDataAdapter(query, db);
                    DataTable dta = new DataTable();
                    sda.Fill(dta);

                    db.Close();

                    MessageBox.Show("Registration Done!", "Success!");

                    LoginForm lf = new LoginForm();
                    this.Hide();
                    lf.Show();
                }
            }
        }

        private bool isValid()
        {
            if (txtName.Text.TrimStart() == string.Empty)
            {
                MessageBox.Show("Please enter your valid name", "Error");
                return false;
            }
            else if (txtPassword.Text.TrimStart() == string.Empty)
            {
                MessageBox.Show("Please enter your valid password", "Error");
                return false;
            }
            else if (txtConfirmPassword.Text.TrimStart() == string.Empty)
            {
                MessageBox.Show("Please enter confirm password", "Error");
                return false;
            }

            if(txtPassword.Text.TrimStart() != txtConfirmPassword.Text.TrimStart())
            {
                MessageBox.Show("Password and confirm password aren't equals", "Error");
                return false;
            }

            return true;
        }

        private bool isLoginExist()
        {
            using (SqlConnection db = new SqlConnection(connectionString))
            {
                string query = $"SELECT * FROM LoginTable WHERE UserName = '{txtName.Text.TrimStart()}'";

                db.Open();

                SqlDataAdapter sda = new SqlDataAdapter(query, db);
                DataTable dta = new DataTable();
                sda.Fill(dta);

                db.Close();

                if(dta.Rows.Count > 0)
                {
                    MessageBox.Show("Error, this login already exist!", "Error!");
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
}<file_sep>/Login App/LoginForm.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Configuration;

namespace Login_App
{
    public partial class LoginForm : Form
    {
        string connectionString = ConfigurationManager.ConnectionStrings["UsersConnection"].ConnectionString;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void LoginBtn_Click(object sender, EventArgs e)
        {
            if(isValid())
            {
                using (SqlConnection db = new SqlConnection(connectionString))
                {

                    db.Open();
                    

                    SqlCommand cmd = new SqlCommand("select count(*) from LoginTable where UserName=@UserName and Password=@Password", db);

                    cmd.Parameters.AddWithValue("@UserName", UserNameTxt.Text.Trim());
                    cmd.Parameters.AddWithValue("@Password", PasswordTxt.Text.Trim());

                    var isCorrectPassword = cmd.ExecuteScalar();


                    if ((int)isCorrectPassword >= 1)
                    {
                        GetRoot();
                        User.UserName = UserNameTxt.Text.Trim();

                        MainWindow main = new MainWindow();
                        this.Hide();
                        main.Show();
                        db.Close();
                    }
                    else
                    {
                        MessageBox.Show("Password not correct", "Error");
                        db.Close();
                    }                   
                }
            }
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void ExitBtn_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private bool isValid()
        {
            if(UserNameTxt.Text.TrimStart() == string.Empty)
            {
                MessageBox.Show("Please enter your valid name", "Error");
                return false;
            }else if (PasswordTxt.Text.TrimStart() == string.Empty)
            {
                MessageBox.Show("Please enter your valid password", "Error");
                return false;
            }
            return true;
        }

        private void GetRoot()
        {
            using(SqlConnection db = new SqlConnection(connectionString))
            {
                db.Open();
                SqlCommand cmd = new SqlCommand($"SELECT Root FROM LoginTable WHERE UserName = '{UserNameTxt.Text.Trim()}' AND Password = '{PasswordTxt.Text.Trim()}'", db);
                var Root = cmd.ExecuteScalar();

                if(string.IsNullOrWhiteSpace(Root.ToString()) || Root.ToString() == null)
                {
                    User.Root = "user";
                }
                else
                {
                    User.Root = Root.ToString();
                }
                db.Close();
            }
        }

        private void btnRegistration_Click(object sender, EventArgs e)
        {
            Registration rg = new Registration();
            this.Hide();
            rg.Show();
        }
    }
}<file_sep>/Login App/User.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Login_App
{
    public static class User
    {
        public static string UserName { get; set; }
        public static string Password { get; set; }
        public static string Root  { get; set; }
       
    }
}
