public static string ShowForm(OpeningResults condition) { PasswordInputForm newPIF = new PasswordInputForm(); returnval = null; if (condition == OpeningResults.PasswordNeeded) { newPIF.label1.Text = "A password is needed to open this level."; } else if (condition == OpeningResults.WrongPassword) { newPIF.label1.Text = "Sorry, wrong password. Please try again."; } newPIF.ShowDialog(); return(returnval); }
private bool ProvideMSAccessPassword() { bool isSuccessful = false; string password = MSAccess.Properties.Settings.Default.StudentElectionConnectionPassword; do { try { var connectionStringBuilder = new StringBuilder(); connectionStringBuilder.Append("Provider=Microsoft.Jet.OLEDB.4.0;"); connectionStringBuilder.Append($@"Data Source=|DataDirectory|\{ MdbFileName };"); if (!string.IsNullOrEmpty(password)) { connectionStringBuilder.Append($@"Jet OLEDB:Database Password={ password };"); } var connectionStringName = nameof(MSAccess.Properties.Settings.Default.StudentElectionConnectionString); MSAccess.Properties.Settings.Default[connectionStringName] = connectionStringBuilder.ToString(); using (var manager = new MSAccess.StudentElectionDataSetTableAdapters.TableAdapterManager()) { manager.Connection = new OleDbConnection(MSAccess.Properties.Settings.Default.StudentElectionConnectionString); manager.Connection.Open(); manager.Connection.Close(); } isSuccessful = true; if (!MSAccess.Properties.Settings.Default.StudentElectionConnectionPassword.Equals(password)) { MSAccess.Properties.Settings.Default.StudentElectionConnectionPassword = password; MSAccess.Properties.Settings.Default.Save(); } break; } catch (Exception ex) { Logger.LogError(ex); if (ex is OleDbException oleDbException && oleDbException.ErrorCode == -2147217843) { MessageBox.Show($"{ ex.GetBaseException().Message }.\n\nPress OK to enter the correct password", "Connection error", MessageBoxButton.OK, MessageBoxImage.Exclamation); var form = new PasswordInputForm($"Type the password for '{ MdbFileName }' file:") { Text = $"Password for '{ MdbFileName }' file", StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen }; var result = form.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { password = form.Password; continue; } else { break; } } else { var tryAgainResult = MessageBox.Show($"Unable to connect with '{ MdbFileName }' file.\n\nDo you want to try again?", "Connection error", MessageBoxButton.YesNo, MessageBoxImage.Exclamation); if (tryAgainResult == MessageBoxResult.No) { break; } } }