private void cmdOK_Click(object sender, EventArgs e)
        {
            SqlTableSizeCollectorEntry selectedEntry = (SqlTableSizeCollectorEntry)SelectedEntry;

            selectedEntry.SqlServer          = txtServer.Text;
            selectedEntry.Database           = cboDatabase.Text;
            selectedEntry.IntegratedSecurity = chkIntegratedSec.Checked;
            selectedEntry.UserName           = txtUserName.Text;
            selectedEntry.Password           = txtPassword.Text;
            selectedEntry.SqlCmndTimeOutSec  = (int)numericUpDownCmndTimeOut.Value;
            selectedEntry.Tables.Clear();
            foreach (ListViewItem lvi in lvwTables.CheckedItems)
            {
                TableSizeEntry tse = new TableSizeEntry()
                {
                    TableName    = lvi.Text,
                    WarningValue = int.Parse(lvi.SubItems[1].Text),
                    ErrorValue   = int.Parse(lvi.SubItems[2].Text),
                    RowCount     = int.Parse(lvi.SubItems[3].Text)
                };
                selectedEntry.Tables.Add(tse);
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        public void FromXml(string configurationString)
        {
            XmlDocument config = new XmlDocument();

            config.LoadXml(configurationString);
            XmlElement root = config.DocumentElement;

            Entries.Clear();
            foreach (XmlElement databaseNode in root.SelectNodes("databases/database"))
            {
                SqlTableSizeCollectorEntry entry = new SqlTableSizeCollectorEntry();
                entry.SqlServer          = databaseNode.ReadXmlElementAttr("server", "");
                entry.Database           = databaseNode.ReadXmlElementAttr("name", "");
                entry.IntegratedSecurity = databaseNode.ReadXmlElementAttr("integratedSec", true);
                entry.UserName           = databaseNode.ReadXmlElementAttr("userName", "");
                entry.Password           = databaseNode.ReadXmlElementAttr("password", "");
                entry.SqlCmndTimeOutSec  = databaseNode.ReadXmlElementAttr("sqlCmndTimeOutSec", 30);
                entry.PrimaryUIValue     = databaseNode.ReadXmlElementAttr("primaryUIValue", true);

                foreach (XmlElement tableNode in databaseNode.SelectNodes("table"))
                {
                    TableSizeEntry tableSizeEntry = new TableSizeEntry();
                    tableSizeEntry.TableName    = tableNode.ReadXmlElementAttr("name", "");
                    tableSizeEntry.WarningValue = long.Parse(tableNode.ReadXmlElementAttr("warningValue", "1"));
                    tableSizeEntry.ErrorValue   = long.Parse(tableNode.ReadXmlElementAttr("errorValue", "2"));
                    entry.Tables.Add(tableSizeEntry);
                }

                Entries.Add(entry);
            }
        }
        private void LoadTables()
        {
            try
            {
                if (txtServer.Text.Trim().Length > 0 && cboDatabase.Text.Length > 0)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    lvwTables.Items.Clear();
                    List <TableSizeInfo> tables = SqlTableSizeCollectorEntry.GetAllTableRowCounts(txtServer.Text, cboDatabase.Text, chkIntegratedSec.Checked, txtUserName.Text, txtPassword.Text, (int)numericUpDownCmndTimeOut.Value);
                    foreach (var table in tables)
                    {
                        ListViewItem lvi = new ListViewItem(table.Name);
                        lvi.SubItems.Add("0");
                        lvi.SubItems.Add("0");
                        lvi.SubItems.Add(table.Rows.ToString());
                        lvi.Tag = table;

                        SqlTableSizeCollectorEntry selectedEntry;
                        if (SelectedEntry != null)
                        {
                            selectedEntry = (SqlTableSizeCollectorEntry)SelectedEntry;
                        }
                        else
                        {
                            selectedEntry = (SqlTableSizeCollectorEntry)SelectedSqlTableSizeEntry;
                        }

                        if (selectedEntry != null && selectedEntry.Tables != null)
                        {
                            TableSizeEntry tse = (from t in selectedEntry.Tables
                                                  where t.TableName.ToLower() == table.Name.ToLower()
                                                  select t).FirstOrDefault();
                            if (tse != null)
                            {
                                lvi.Checked          = true;
                                lvi.SubItems[1].Text = tse.WarningValue.ToString();
                                lvi.SubItems[2].Text = tse.ErrorValue.ToString();
                            }
                        }
                        lvwTables.Items.Add(lvi);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loading tables", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Cursor.Current = Cursors.Default;
        }
 public override void RefreshDisplayData()
 {
     foreach (ListViewGroup lvg in lvwEntries.Groups)
     {
         SqlTableSizeCollectorEntry tableSizeEntry             = (SqlTableSizeCollectorEntry)lvg.Tag;
         List <Tuple <TableSizeEntry, CollectorState> > states = new List <Tuple <TableSizeEntry, CollectorState> >();
         try
         {
             tableSizeEntry.RefreshRowCounts();
             states = tableSizeEntry.GetStates();
         }
         catch { }
         foreach (ListViewItem lvi in lvg.Items)
         {
             TableSizeEntry tableEntry = (TableSizeEntry)lvi.Tag;
             if (tableEntry.RowCount > -1)
             {
                 lvi.SubItems[1].Text = tableEntry.RowCount.ToString();
             }
             else
             {
                 lvi.SubItems[1].Text = tableEntry.ErrorStr;
             }
             CollectorState currentState = (from s in states
                                            where s.Item1.TableName == tableEntry.TableName
                                            select s.Item2).FirstOrDefault();
             if (currentState == CollectorState.Good)
             {
                 lvi.ImageIndex = 0;
             }
             else if (currentState == CollectorState.Warning)
             {
                 lvi.ImageIndex = 1;
             }
             else
             {
                 lvi.ImageIndex = 2;
             }
         }
     }
 }