public static bool login(user ExisitingUser, MetroForm ui)
 {
     try
     {
         row = UserDataTable.FindByusername(ExisitingUser.username);
         if (row == null)
         {
             MSG.ERROR(ui, "User Doesn't Exists in the System");
             return(false);
         }
         if ((row.username.ToString() == ExisitingUser.username) && (row.password.ToString() == ExisitingUser.password))
         {
             MSG.SUCCESS(ui, "Login Success.!");
             ui.Hide();
             frm_dashboard n = new frm_dashboard();
             n.ShowDialog();
             ui.Close();
             return(true);
         }
         else
         {
             MSG.ERROR(ui, "Username and Password Doesn't Match!");
             return(false);
         }
     }
     catch (MySql.Data.MySqlClient.MySqlException ex)
     {
         MSG.ERROR(ui, "Login Error.!, " + ex.Message);
         return(false);
     }
 }
        public static bool reset(user ExisitingUser, string NewPassword, MetroForm ui)
        {
            try
            {
                row = UserDataTable.FindByusername(ExisitingUser.username);
                if (row == null)
                {
                    MSG.ERROR(ui, "Invalid Username...!");
                    return(false);
                }

                else if (row.nic != ExisitingUser.nic)
                {
                    MSG.ERROR(ui, "Invalid NIC...!");
                    return(false);
                }
                else if (row.contactNumber != ExisitingUser.contactNumber)
                {
                    MSG.ERROR(ui, "Invalid Contact Number...!");
                    return(false);
                }
                else if (NewPassword == "")
                {
                    MSG.ERROR(ui, "Password Can not be empty......!");
                    return(false);
                }
                else if (row.username == ExisitingUser.username && row.nic == ExisitingUser.nic && row.contactNumber == ExisitingUser.contactNumber)
                {
                    UserTable.UpdatePasswordByUsername(NewPassword, ExisitingUser.username);
                    MSG.SUCCESS(ui, "Password Reset Success.!");
                    UserDataTable.Clear();
                    UserDataTable = UserTable.GetData();
                    return(true);
                }
                else
                {
                    MSG.ERROR(ui, "Password Reset Error.!");
                    return(false);
                }
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                MSG.ERROR(ui, "Password Reset Error.Please Try again..." + ex.Message);
                return(false);
            }
        }
        public static void UpdatePassword(MetroForm form, string username, string password, string confirmPassword, string Contact, string nic)
        {
            try
            {
                row = userTable.FindByUsername(username);
                if (row == null)
                {
                    CommonFunctions.WriteUserLog(Session.Username, "Entered wrong Username");
                    CommonFunctions.ShowError(form, "wrong Username");
                }
                else if (nic != row.Nic)
                {
                    CommonFunctions.WriteUserLog(Session.Username, "Entered wrong Nic");
                    CommonFunctions.ShowError(form, "wrong NIC");
                }
                else if (Contact != row.ContactNumber)
                {
                    CommonFunctions.WriteUserLog(Session.Username, "Entered wrong Contact  Number ");
                    CommonFunctions.ShowError(form, "Wrong contact number");
                }
                else if (password == "")
                {
                    CommonFunctions.WriteUserLog(Session.Username, "Entered without value ");
                    CommonFunctions.ShowError(form, "Please Enter password");
                }
                else if (password != confirmPassword)
                {
                    CommonFunctions.WriteUserLog(Session.Username, "Entered wrong Password");
                    CommonFunctions.ShowError(form, "Passwords does not match");
                }

                else
                {
                    usersAdapter.UpdateUserPasswordByUsername(password, username);
                    CommonFunctions.WriteUserLog(Session.Username, "Changed the Password of " + username);
                    CommonFunctions.ShowSuccess(form, "Changed the Password of " + username);
                }
            }
            catch (Exception ex)
            {
                CommonFunctions.WriteToErrorLog(ex.Message);
                CommonFunctions.ShowError(form, "Can not Change Password.");
            }
        }
        public static void login(MetroForm form)
        {
            try
            {
                usersAdapter.Fill(userTable);
                if ((User.Username == "") || (User.Password == ""))
                {
                    CommonFunctions.ShowError(form, "Please Enter valid username or Password.Fields can not be empty...");
                    CommonFunctions.WriteUserLog("SYSTEM", " Some User Tried to Login with Null username or Password");
                }
                row = userTable.FindByUsername(User.Username);
                if (row == null)
                {
                    //user not exists
                    CommonFunctions.ShowError(form, "User does not Exists.");
                    CommonFunctions.WriteUserLog("SYSTEM", "Failed Login Attemp With Username -> " + User.Username);
                    //return false;
                }

                else if ((row != null) && (User.Password != row.Password))
                {
                    //wrong password

                    CommonFunctions.ShowError(form, "Wrong Password.Please Check and Re enter...");
                    CommonFunctions.WriteUserLog("SYSTEM", User.Username + " Tried to Login with Wrong Password");

                    //return false;
                }
                else if ((User.Username == row.Username) && (row.Password == User.Password) && (row.IsActive == 0))
                {
                    //Disabled User

                    CommonFunctions.ShowError(form, "This User can not Login in to the System. Access Denied...");
                    CommonFunctions.WriteUserLog("SYSTEM", "A Disabled User Tried to Login > " + User.Username);

                    //return false;
                }

                else if ((User.Username == row.Username) && (row.Password == User.Password))
                {
                    //login success
                    CommonFunctions.ShowSuccess(form, "Login Successfully...");
                    Session.Username             = row.Username;
                    Session.BranchId             = row.BranchId;
                    Brow                         = branchTable.FindById(row.BranchId);
                    Session.BranchMasterPassword = Brow.MasterPassword;
                    Session.BranchName           = Brow.BranchName;
                    CommonFunctions.WriteUserLog("SYSTEM", User.Username + " is Log into the System");

                    new UI_DASHBOARD(form).Show();
                    form.Hide();
                    form.ShowInTaskbar = false;


                    //return true;
                }
            }
            catch (Exception ex)
            {
                //show some error and log the original error!
                CommonFunctions.ShowError(form, ex.ToString());
                CommonFunctions.WriteToErrorLog(ex.Message.ToString());
                //return false;
            }
        }