From a67dd966bde9fb181ac8949dad3979154f71a72e Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 12 Apr 2017 20:01:32 -0500 Subject: [PATCH] Created a "login" dialog form to grab the server name from the user. Now a valid connection must be made before the Main form loads. --- .../AdvertisingProfitControlModel.cs | 4 +- .../AdvertsingProfitControl.csproj | 9 ++ AdvertsingProfitControl/App.config | 10 ++ .../FrmDatabaseSelector.Designer.cs | 108 ++++++++++++++++ .../FrmDatabaseSelector.cs | 37 ++++++ .../FrmDatabaseSelector.resx | 120 ++++++++++++++++++ AdvertsingProfitControl/Program.cs | 17 ++- .../Properties/Settings.Designer.cs | 14 +- .../Properties/Settings.settings | 8 +- 9 files changed, 317 insertions(+), 10 deletions(-) create mode 100644 AdvertsingProfitControl/FrmDatabaseSelector.Designer.cs create mode 100644 AdvertsingProfitControl/FrmDatabaseSelector.cs create mode 100644 AdvertsingProfitControl/FrmDatabaseSelector.resx diff --git a/AdvertsingProfitControl/AdvertisingProfitControlModel.cs b/AdvertsingProfitControl/AdvertisingProfitControlModel.cs index 1e911d1..860d4a9 100644 --- a/AdvertsingProfitControl/AdvertisingProfitControlModel.cs +++ b/AdvertsingProfitControl/AdvertisingProfitControlModel.cs @@ -1,3 +1,5 @@ +using AdvertsingProfitControl.Properties; + namespace AdvertsingProfitControl { using System; @@ -8,7 +10,7 @@ namespace AdvertsingProfitControl public partial class AdvertisingProfitControlModel : DbContext { public AdvertisingProfitControlModel() - : base("name=AdvertisingProfitControlDbContext") + : base(Settings.Default.ConnectionString) { } diff --git a/AdvertsingProfitControl/AdvertsingProfitControl.csproj b/AdvertsingProfitControl/AdvertsingProfitControl.csproj index 80866a2..083e790 100644 --- a/AdvertsingProfitControl/AdvertsingProfitControl.csproj +++ b/AdvertsingProfitControl/AdvertsingProfitControl.csproj @@ -128,6 +128,12 @@ DebugDatabaseConverter.cs + + Form + + + FrmDatabaseSelector.cs + Form @@ -193,6 +199,9 @@ FrmAddRecord.cs + + FrmDatabaseSelector.cs + FrmMain.cs diff --git a/AdvertsingProfitControl/App.config b/AdvertsingProfitControl/App.config index 1dd3bd0..eb9c931 100644 --- a/AdvertsingProfitControl/App.config +++ b/AdvertsingProfitControl/App.config @@ -3,6 +3,9 @@
+ +
+ @@ -17,4 +20,11 @@ + + + + + + + \ No newline at end of file diff --git a/AdvertsingProfitControl/FrmDatabaseSelector.Designer.cs b/AdvertsingProfitControl/FrmDatabaseSelector.Designer.cs new file mode 100644 index 0000000..dfd6152 --- /dev/null +++ b/AdvertsingProfitControl/FrmDatabaseSelector.Designer.cs @@ -0,0 +1,108 @@ +namespace AdvertsingProfitControl +{ + partial class FrmDatabaseSelector + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.serverNameTextBox = new System.Windows.Forms.TextBox(); + this.label3 = new System.Windows.Forms.Label(); + this.button1 = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(1, 9); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(35, 13); + this.label1.TabIndex = 0; + this.label1.Text = "label1"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(13, 166); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(69, 13); + this.label2.TabIndex = 1; + this.label2.Text = "Server Name"; + // + // serverNameTextBox + // + this.serverNameTextBox.Location = new System.Drawing.Point(88, 163); + this.serverNameTextBox.Name = "serverNameTextBox"; + this.serverNameTextBox.Size = new System.Drawing.Size(180, 20); + this.serverNameTextBox.TabIndex = 2; + this.serverNameTextBox.Text = "(LocalDb)\\MSSQLLocalDB"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(16, 194); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(35, 13); + this.label3.TabIndex = 3; + this.label3.Text = "label3"; + // + // button1 + // + this.button1.Location = new System.Drawing.Point(354, 270); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(105, 23); + this.button1.TabIndex = 4; + this.button1.Text = "button1"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // FrmDatabaseSelector + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(486, 319); + this.Controls.Add(this.button1); + this.Controls.Add(this.label3); + this.Controls.Add(this.serverNameTextBox); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Name = "FrmDatabaseSelector"; + this.Text = "FrmDatabaseSelector"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox serverNameTextBox; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Button button1; + } +} \ No newline at end of file diff --git a/AdvertsingProfitControl/FrmDatabaseSelector.cs b/AdvertsingProfitControl/FrmDatabaseSelector.cs new file mode 100644 index 0000000..a9e595d --- /dev/null +++ b/AdvertsingProfitControl/FrmDatabaseSelector.cs @@ -0,0 +1,37 @@ +using System; +using System.Data.SqlClient; +using System.Windows.Forms; +using AdvertsingProfitControl.Properties; + +namespace AdvertsingProfitControl +{ + public partial class FrmDatabaseSelector : Form + { + public bool _connectionSuccessful = false; + + public FrmDatabaseSelector() + { + InitializeComponent(); + } + + private void button1_Click(object sender, EventArgs e) + { + var connectionString = "Data Source=" + serverNameTextBox.Text + ";" + "Initial Catalog=AdvertisingProfitControl;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework"; + try + { + //Test the connection string with a forced timeout of 2 seconds. + using (var conn = new SqlConnection(connectionString + "Connection Timeout=2")) + { + conn.Open(); // throws if invalid + } + Settings.Default.ConnectionString = connectionString; + _connectionSuccessful = true; + this.Close(); + } + catch (Exception ex) + { + label1.Text = ex.Message; + } + } + } +} diff --git a/AdvertsingProfitControl/FrmDatabaseSelector.resx b/AdvertsingProfitControl/FrmDatabaseSelector.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/AdvertsingProfitControl/FrmDatabaseSelector.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/AdvertsingProfitControl/Program.cs b/AdvertsingProfitControl/Program.cs index 9f62615..8993dc0 100644 --- a/AdvertsingProfitControl/Program.cs +++ b/AdvertsingProfitControl/Program.cs @@ -19,13 +19,18 @@ namespace AdvertsingProfitControl //var errorConsoleThread = new Thread(console.Show); //errorConsoleThread.IsBackground = true; //errorConsoleThread.Start(); - if (args.Length == 1 && args[0] == "/debug") + var form = new FrmDatabaseSelector(); + form.ShowDialog(); + if (form._connectionSuccessful) { - Application.Run(new FrmMain(true)); - } - else - { - Application.Run(new FrmMain(false)); + if (args.Length == 1 && args[0] == "/debug") + { + Application.Run(new FrmMain(true)); + } + else + { + Application.Run(new FrmMain(false)); + } } } } diff --git a/AdvertsingProfitControl/Properties/Settings.Designer.cs b/AdvertsingProfitControl/Properties/Settings.Designer.cs index 162fc25..c4e1dab 100644 --- a/AdvertsingProfitControl/Properties/Settings.Designer.cs +++ b/AdvertsingProfitControl/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace AdvertsingProfitControl.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.0.1.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -22,5 +22,17 @@ namespace AdvertsingProfitControl.Properties { return defaultInstance; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string ConnectionString { + get { + return ((string)(this["ConnectionString"])); + } + set { + this["ConnectionString"] = value; + } + } } } diff --git a/AdvertsingProfitControl/Properties/Settings.settings b/AdvertsingProfitControl/Properties/Settings.settings index 8e615f2..b818075 100644 --- a/AdvertsingProfitControl/Properties/Settings.settings +++ b/AdvertsingProfitControl/Properties/Settings.settings @@ -1,5 +1,9 @@  - + - + + + + + \ No newline at end of file