using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using AnywhereTS.DBSupport; using Microsoft.Win32; namespace AnywhereTS { public partial class frmConfigureControlPanel : Form { public frmConfigureControlPanel() { InitializeComponent(); } private void GetAnywhereTSDatabase(string ATSServer, out string DBServer, out string DBInstance) { DBServer = string.Empty; DBInstance = string.Empty; try { Logging.DatabaseLog.InfoFormat("Getting AnywhereTS Database Server and Instance from: {0}", ATSServer); RegistryKey rk = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ATSServer); rk = rk.OpenSubKey(string.Format(@"SOFTWARE\{0}\ts-config", ATSGlobals.ApplicationName)); DBServer = (string)rk.GetValue(ProSupport.strRegDatabaseServer); DBInstance = (string)rk.GetValue(ProSupport.strRegDatabaseInstance); Logging.DatabaseLog.DebugFormat("DBServer={0} DBInstance={1}", DBServer, DBInstance); Logging.DatabaseLog.InfoFormat("Successfully retreived AnywhereTS Database Server and Instance from: {0}", ATSServer); } catch (Exception ex) { using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) { Logging.DatabaseLog.Error(string.Format("Failed to open Database {0}", ATSGlobals.strDatabaseName)); } } } private void btnOk_Click(object sender, EventArgs e) { // Validate database server string dataServer; dataServer = textInput.Text.Trim(); string DBServer = ""; string DBInstance = ""; GetAnywhereTSDatabase(dataServer, out DBServer, out DBInstance); ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseServer, DBServer); ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseInstance, DBInstance); ProSupport.strDatabaseServer = DBServer; ProSupport.strDatabaseInstance = DBInstance; //Create a connection to SQL Server Exception ErrorInfo = null; Logging.ATSAdminLog.Debug("frmConfigureControlPanel.btnOk_Click() called "); using (MsSqlConnector conn = new MsSqlConnector(ProSupport.strDatabaseServer, ProSupport.strDatabaseInstance, "master")) { try { Logging.ATSAdminLog.Info(string.Format("Opening Database {0}", ATSGlobals.strDatabaseName)); conn.CreateConnection(out ErrorInfo); conn.OpenConnection(out ErrorInfo); Logging.ATSAdminLog.Info(string.Format("Opened Database {0}", ATSGlobals.strDatabaseName)); } catch (SqlException ex) { using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) { Logging.DatabaseLog.Error(string.Format("Failed to open Database {0}", ATSGlobals.strDatabaseName)); } ErrorInfo = ex; throw ErrorInfo; } catch (Exception ex) { using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) { Logging.DatabaseLog.Error(string.Format("Failed to open Database {0}", ATSGlobals.strDatabaseName)); } ErrorInfo = ex; throw ErrorInfo; } } if (ErrorInfo != null) { MessageBox.Show(string.Format("Failed to open database {0}. Please check the log for errors.", ATSGlobals.strDatabaseName)); } // Save database server to variable and registry ProSupport.strAnywhereTSServer = dataServer; ATSGlobals.SetATSRegValue(ProSupport.strRegAnywhereTSServer, ProSupport.strAnywhereTSServer); DialogResult = DialogResult.OK; //return } } }