private void butSyncManual_Click(object sender, EventArgs e)
        {
            if (gridMain.SelectedIndices.Length == 0)
            {
                MsgBox.Show(this, "Please select at least one middle tier connection.");
                return;
            }
            List <Tuple <Userod, string> > listUsers = new List <Tuple <Userod, string> >();  //Middle tier requires Userod and PasswordTyped

            listUsers.Add(Tuple.Create(Security.CurUser, Security.PasswordTyped));
            for (int i = 0; i < gridMain.SelectedIndices.Length; i++)
            {
                if (((CentralConnection)gridMain.Rows[gridMain.SelectedIndices[i]].Tag).ServiceURI == "")
                {
                    continue;
                }
                bool isSynced = false;
                for (int j = 0; j < listUsers.Count; j++)
                {
                    Security.CurUser       = listUsers[j].Item1;
                    Security.PasswordTyped = listUsers[j].Item2;
                    CentralSyncHelper.SyncAll(new List <CentralConnection>()
                    {
                        (CentralConnection)gridMain.Rows[gridMain.SelectedIndices[i]].Tag
                    });
                    if (CentralSyncHelper.ListSyncErrors.Count == 0)
                    {
                        isSynced = true;
                    }
                    else
                    {
                        FormCentralLogOn FormCLO = new FormCentralLogOn();
                        FormCLO.IsMiddleTierSync = true;
                        if (FormCLO.ShowDialog() == DialogResult.OK)
                        {
                            listUsers.Add(Tuple.Create(Security.CurUser, Security.PasswordTyped));
                            CentralSyncHelper.SyncAll(new List <CentralConnection>()
                            {
                                (CentralConnection)gridMain.Rows[gridMain.SelectedIndices[i]].Tag
                            });                                                                                                                                                         //Try again
                            if (CentralSyncHelper.ListSyncErrors.Count == 0)
                            {
                                isSynced = true;
                            }
                        }
                    }
                    if (isSynced)
                    {
                        break;
                    }
                }
                //Set user and password back to what it was originally
                Security.CurUser       = listUsers[0].Item1;
                Security.PasswordTyped = listUsers[0].Item2;
            }
        }
        private void menuItemLogoff_Click(object sender, EventArgs e)
        {
            FormCentralLogOn FormCLO = new FormCentralLogOn();

            if (FormCLO.ShowDialog() != DialogResult.OK)
            {
                Application.Exit();
                return;
            }
            this.Text = "Central Manager - " + Security.CurUser.UserName;
        }
        ///<summary>Gets the settings from the config file and attempts to connect.</summary>
        private bool GetConfigAndConnect()
        {
            string xmlPath = Path.Combine(Application.StartupPath, "CentralManagerConfig.xml");

            if (!File.Exists(xmlPath))
            {
                MessageBox.Show("Please create CentralManagerConfig.xml according to the manual before using this tool.");
                _hasFatalError = true;
                Application.Exit();
                return(false);
            }
            XmlDocument document     = new XmlDocument();
            string      computerName = "";
            string      database     = "";
            string      user         = "";
            string      password     = "";
            string      middleTier   = "";

            try {
                document.Load(xmlPath);
                XPathNavigator Navigator = document.CreateNavigator();
                XPathNavigator nav;
                DataConnection.DBtype = DatabaseType.MySql;
                //See if there's a DatabaseConnection
                nav = Navigator.SelectSingleNode("//DatabaseConnection");
                if (nav == null)
                {
                    MessageBox.Show("DatabaseConnection element missing from CentralManagerConfig.xml.");
                    Application.Exit();
                    return(false);
                }
                computerName = nav.SelectSingleNode("ComputerName").Value;
                database     = nav.SelectSingleNode("Database").Value;
                user         = nav.SelectSingleNode("User").Value;
                password     = nav.SelectSingleNode("Password").Value;
                XPathNavigator passHashNode = nav.SelectSingleNode("MySQLPassHash");
                string         decryptedPwd;
                if (password == "" && passHashNode != null && passHashNode.Value != "" && CDT.Class1.Decrypt(passHashNode.Value, out decryptedPwd))
                {
                    password = decryptedPwd;
                }
                XPathNavigator nodeMT = nav.SelectSingleNode("MiddleTierAddr");
                if (nodeMT != null)
                {
                    middleTier = nodeMT.Value;
                }
            }
            catch (Exception ex) {
                //Common error: root element is missing
                MessageBox.Show(ex.Message);
                Application.Exit();
                return(false);
            }
            DataConnection.DBtype = DatabaseType.MySql;
            OpenDentBusiness.DataConnection dcon = new OpenDentBusiness.DataConnection();
            //Try to connect to the database directly
            if (middleTier != "")
            {
                FormCentralChooseDatabase FormCCD = new FormCentralChooseDatabase(middleTier);
                if (FormCCD.ShowDialog() == DialogResult.Cancel)
                {
                    Application.Exit();
                    return(false);
                }
            }
            else
            {
                try {
                    dcon.SetDb(computerName, database, user, password, "", "", DataConnection.DBtype);
                    RemotingClient.RemotingRole = RemotingRole.ClientDirect;
                    FormCentralLogOn FormCLO = new FormCentralLogOn();
                    if (FormCLO.ShowDialog() != DialogResult.OK)
                    {
                        _hasFatalError = true;
                        Application.Exit();
                        return(false);
                    }
                    return(true);
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message);
                    _hasFatalError = true;
                    Application.Exit();
                    return(false);
                }
            }
            return(true);
        }