private void LoadDetailView()
        {
            lvwDetails.BeginUpdate();
            lvwDetails.Items.Clear();
            lvwDetails.Columns.Clear();
            if (lvwResults.SelectedItems.Count >= 1 && lvwResults.SelectedItems[0].Tag is QueryInstance)
            {
                QueryInstance queryInstance = (QueryInstance)lvwResults.SelectedItems[0].Tag;
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    DataSet ds = queryInstance.RunDetailQuery();
                    foreach (DataColumn currentDataColumn in ds.Tables[0].Columns)
                    {
                        ColumnHeader newColumn = new ColumnHeader();
                        newColumn.Tag  = currentDataColumn;
                        newColumn.Text = currentDataColumn.Caption;

                        if ((currentDataColumn.DataType == typeof(UInt64)) || (currentDataColumn.DataType == typeof(UInt32)) || (currentDataColumn.DataType == typeof(UInt16)) ||
                            (currentDataColumn.DataType == typeof(Int64)) || (currentDataColumn.DataType == typeof(Int32)) || (currentDataColumn.DataType == typeof(Int16)))
                        {
                            newColumn.TextAlign = HorizontalAlignment.Right;
                        }
                        else
                        {
                            newColumn.TextAlign = HorizontalAlignment.Left;
                        }
                        lvwDetails.Columns.Add(newColumn);
                    }
                    foreach (DataRow r in ds.Tables[0].Rows)
                    {
                        ListViewItem lvi = new ListViewItem(FormatUtils.N(r[0], "[Null]"));
                        for (int i = 1; i < lvwDetails.Columns.Count; i++)
                        {
                            lvi.SubItems.Add(FormatUtils.N(r[i], "[Null]"));
                        }
                        lvwDetails.Items.Add(lvi);
                    }
                    lvwDetails.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "View details", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
            lvwDetails.EndUpdate();
            ExportButtonVisible = lvwResults.SelectedItems.Count > 0;
        }
        private void cmdTest_Click(object sender, EventArgs e)
        {
            if (DoValidate())
            {
                string lastStep = "Initialize values";
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    QueryInstance testQueryInstance = new QueryInstance();
                    testQueryInstance.Name               = txtName.Text;
                    testQueryInstance.SqlServer          = txtServer.Text;
                    testQueryInstance.Database           = txtDatabase.Text;
                    testQueryInstance.IntegratedSecurity = chkIntegratedSec.Checked;
                    testQueryInstance.UserName           = txtUserName.Text;
                    testQueryInstance.Password           = txtPassword.Text;
                    testQueryInstance.CmndTimeOut        = (int)numericUpDownCmndTimeOut.Value;
                    testQueryInstance.UseSPForSummary    = chkUseSPForSummary.Checked;
                    testQueryInstance.SummaryQuery       = txtStateQuery.Text;
                    if (cboReturnType.SelectedIndex == 0)
                    {
                        testQueryInstance.ReturnValueIsNumber   = false;
                        testQueryInstance.UseRowCountAsValue    = false;
                        testQueryInstance.UseExecuteTimeAsValue = false;
                    }
                    else
                    {
                        testQueryInstance.ReturnValueIsNumber = true;
                        if (cboReturnType.SelectedIndex == 2)
                        {
                            testQueryInstance.UseRowCountAsValue = true;
                        }
                        else if (cboReturnType.SelectedIndex == 3)
                        {
                            testQueryInstance.UseExecuteTimeAsValue = true;
                            chkReturnValueNotInverted.Checked       = true;
                        }
                    }
                    testQueryInstance.ReturnValueInverted = !chkReturnValueNotInverted.Checked;
                    testQueryInstance.SuccessValue        = cboSuccessValue.Text;
                    testQueryInstance.WarningValue        = cboWarningValue.Text;
                    testQueryInstance.ErrorValue          = cboErrorValue.Text;
                    testQueryInstance.UseSPForDetail      = chkUseSPForDetail.Checked;
                    testQueryInstance.DetailQuery         = txtDetailQuery.Text;
                    testQueryInstance.ApplicationName     = txtApplicationName.Text;

                    lastStep = "Run summary query";
                    object returnValue = testQueryInstance.RunQuery();
                    if (!testQueryInstance.UseRowCountAsValue && !testQueryInstance.UseExecuteTimeAsValue && !returnValue.IsNumber())
                    {
                        throw new Exception(string.Format("Return value is not an integer!\r\nValue returned: {0}", returnValue));
                    }

                    //if (!testQueryInstance.ReturnValueIsNumber)
                    //{
                    //    lastStep = "Run summary query";
                    //    returnValue = testQueryInstance.RunQueryWithSingleResult();
                    //}
                    //else if (!testQueryInstance.UseRowCountAsValue && !testQueryInstance.UseExecuteTimeAsValue)
                    //{
                    //    lastStep = "Run summary query (value is number)";
                    //    returnValue = testQueryInstance.RunQueryWithSingleResult();
                    //    if (!returnValue.IsNumber())
                    //        throw new Exception(string.Format("Return value is not an integer!\r\nValue returned: {0}", returnValue));
                    //}
                    //else if (testQueryInstance.UseRowCountAsValue)
                    //{
                    //    lastStep = "Run summary query (row count as value)";
                    //    returnValue = testQueryInstance.RunQueryWithCountResult();
                    //}
                    //else
                    //{
                    //    lastStep = "Run summary query (execution time as value)";
                    //    returnValue = testQueryInstance.RunQueryWithExecutionTimeResult();
                    //}

                    //testing detail query
                    List <DataColumn> columns = new List <DataColumn>(); // = testQueryInstance.GetDetailQueryColumns();
                    lastStep = "Testing detail query";
                    DataSet ds = testQueryInstance.RunDetailQuery();
                    lastStep = "Testing detail query - Getting column names";
                    columns.AddRange((from DataColumn c in ds.Tables[0].Columns
                                      select c).ToArray());

                    MessageBox.Show(string.Format("Success!\r\nSummary value return: {0}\r\nDetail row count: {1}\r\nDetail columns returned: {2}", returnValue, ds.Tables[0].Rows.Count, columns.ToCSVString()), "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Failed!\r\nLast step: {0}\r\n{1}", lastStep, ex.Message), "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
        }