/// /// Copyright (c) 2013-2016 Sensus Metering Systems /// using System; using System.Windows.Forms; using TBF.Resources; namespace TBF.Forms { /// /// Dialog to process test bench name and database settings (BenchSettings) /// public partial class DatabaseSettingsDlg : Form { // Reference to bench DB settins (not a local copy) Config.DatabaseSettings bench; /// /// Constructor /// /// Test bench database settings public DatabaseSettingsDlg(Config.DatabaseSettings bench) { this.bench = bench; InitializeComponent(); } void Localize() { Text = Strings.Test_Bench_Databases_Specification; label1.Text = Strings.Test_bench_name; realBenchCheckBox.Text = Strings.Test_bench_is_controlled_by_this_computer; dbTypeLabel.Text = Strings.Database_type; connectionStringLabel.Text = Strings.Connection_string; configLabel.Text = Strings.Configuration; resultsLabel.Text = Strings.Results; usersLabel.Text = Strings.Users; testConnConfigDbBtn.Text = Strings.Test_connection; testConnResultsDbBtn.Text = Strings.Test_connection; testConnUsersDbBtn.Text = Strings.Test_connection; createConfigDbBtn.Text = Strings.Create_DB; createResultsDbBtn.Text = Strings.Create_DB; createUsersDbBtn.Text = Strings.Create_DB; okButton.Text = Strings.OkBtnText; cancelButton.Text = Strings.CancelBtnText; } private void BenchSettingsDlg_Load(object sender, EventArgs e) { Localize(); benchNameTextBox.Text = bench.BenchName; realBenchCheckBox.Checked = bench.IsRealBench; /// Initialize 'Database type' combo-boxes for (int i = 0; i < (int)Config.Entities.DBType.DbTypesCount; i++) { Config.Entities.DBType dbType = (Config.Entities.DBType)i; configDbTypeComboBox.Items.Add(dbType); resultsDbTypeComboBox.Items.Add(dbType); usersDbTypeComboBox.Items.Add(dbType); } configDbTypeComboBox.SelectedIndex = (int)bench.ProceduresDBSettings.DbType; resultsDbTypeComboBox.SelectedIndex = (int)bench.WaterMetersDBSettings.DbType; usersDbTypeComboBox.SelectedIndex = (int)bench.UsersDBSettings.DbType; /// Initialize connection strings configDbConnectionStringTextBox.Text = bench.ProceduresDBSettings.ConnectionString; resultsDbConnectionStringTextBox.Text = bench.WaterMetersDBSettings.ConnectionString; usersDbConnectionStringTextBox.Text = bench.UsersDBSettings.ConnectionString; } bool UpdateBenchFromUIControls() { /// TODO: The following code doesnot work as expected - error message shown! //for (int i = 0; i < Program.LocalSettings.BenchesCount; i++) //{ // if (bench != Program.LocalSettings.TestBenches[i] && // benchNameTextBox.Text == Program.LocalSettings.TestBenches[i].BenchName) // { // MessageBox.Show(Strings.Bench_name_conflict_Use_another_name_pls, Strings.Error, MessageBoxButtons.OK); // return false; // } //} bench.BenchName = benchNameTextBox.Text; bench.IsRealBench = realBenchCheckBox.Checked; bench.ProceduresDBSettings.ConnectionString = configDbConnectionStringTextBox.Text; bench.WaterMetersDBSettings.ConnectionString = resultsDbConnectionStringTextBox.Text; bench.UsersDBSettings.ConnectionString = usersDbConnectionStringTextBox.Text; bench.ProceduresDBSettings.DbType = (Config.Entities.DBType)configDbTypeComboBox.SelectedIndex; bench.WaterMetersDBSettings.DbType = (Config.Entities.DBType)resultsDbTypeComboBox.SelectedIndex; bench.UsersDBSettings.DbType = (Config.Entities.DBType)usersDbTypeComboBox.SelectedIndex; return true; } /// /// Update bench DB setting, return 'OK' /// /// Not used /// Not used private void okButton_Click(object sender, EventArgs e) { if (UpdateBenchFromUIControls()) { DialogResult = DialogResult.OK; Close(); return; } DialogResult = DialogResult.None; } /// /// Throw away changes, return 'Cancel' /// /// Not used /// Not used private void cancelButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } private void createConfigDbBtn_Click(object sender, EventArgs e) { if (!UpdateBenchFromUIControls()) return; if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine + Strings.Do_you_want_to_proceed, Strings.Warning, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)) try { Cursor.Current = Cursors.WaitCursor; Config.FluentCommon.CreateEmptyConfigDB(bench.ProceduresDBSettings); MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification); Cursor.Current = Cursors.Default; DialogResult = DialogResult.None; } catch (Exception exception) { Cursor.Current = Cursors.Default; MessageBox.Show(Strings.Error_while_creating_DB_Reason + exception.Message, Strings.Error); } } private void createResultsDbBtn_Click(object sender, EventArgs e) { if (!UpdateBenchFromUIControls()) return; if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine + Strings.Do_you_want_to_proceed, Strings.Warning, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)) try { Cursor.Current = Cursors.WaitCursor; Results.DB.DbType = bench.WaterMetersDBSettings.DbType; Results.DB.ConnectionString = bench.WaterMetersDBSettings.ConnectionString; Results.DB.CreateEmptyDB(); MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification); Cursor.Current = Cursors.Default; DialogResult = DialogResult.None; } catch (Exception exception) { Cursor.Current = Cursors.Default; string msg = Strings.Error_while_creating_DB_Reason + Environment.NewLine + exception.Message; if (exception.InnerException != null) { msg += Environment.NewLine + exception.InnerException.Message; } MessageBox.Show(msg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void createUsersDbBtn_Click(object sender, EventArgs e) { if (!UpdateBenchFromUIControls()) return; if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine + Strings.Do_you_want_to_proceed, Strings.Warning, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)) try { Cursor.Current = Cursors.WaitCursor; Users.DB.DbType = bench.UsersDBSettings.DbType; Users.DB.ConnectionString = bench.UsersDBSettings.ConnectionString; Users.DB.CreateEmptyDB(); MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification); Cursor.Current = Cursors.Default; DialogResult = DialogResult.None; } catch (Exception exception) { Cursor.Current = Cursors.Default; string msg = Strings.Error_while_creating_DB_Reason + Environment.NewLine + exception.Message; if (exception.InnerException != null) { msg += Environment.NewLine + exception.InnerException.Message; } MessageBox.Show(msg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void testConnConfigDbBtn_Click(object sender, EventArgs e) { } private void testConnResultsDbBtn_Click(object sender, EventArgs e) { } private void testConnUsersDbBtn_Click(object sender, EventArgs e) { } } }