//
        // When called, this will execute the matching query in the current campaign
        // and build the information required for the QueryViewer to interact with it.
        //
        public void RunQuery(Campaign campaign, int queryID, int sortOn, int sortColumn, int sortDirection)
        {
            this.campaign = campaign;
            dmo           = DataManagerOption.GetInstance(campaign.Id, queryID, true);
            Query query = new Query(campaign, queryID);

            conn = new SqlConnection(campaign.ConnString);
            conn.Open();
            string        queryStr = setupQuerySorting(query.QueryCondition, sortOn, sortColumn, sortDirection);
            SqlCommand    cmd      = new SqlCommand(queryStr, conn);
            SqlDataReader sdr      = cmd.ExecuteReader();

            contents  = GetColumnHeaders(sdr);
            rowCount  = 1;
            rowKeys   = "rowKeys=new Array();\n";
            contents += "<tbody>\n";
            string curRow = "";

            while (sdr.Read() && (rowCount <= dmo.RowLimit))
            {
                // Operate with a 1 row delay so we can detect the last row
                contents += curRow;
                curRow    = GetRow(sdr);
            }
            contents += "</tbody><tfoot>";
            contents += curRow;
            contents += "</tfoot>";
            sdr.Close();
            cmd      = new SqlCommand(countRowsQuery(query.QueryCondition), conn);
            rowCount = (int)cmd.ExecuteScalar();
            conn.Close();
        }
        // Creates a CSV view of the data
        //public string CreateCSVView(int queryID, string connString)
        public string CreateCSVView(Campaign campaign, int queryID)
        {
            dmo = DataManagerOption.GetInstance(campaign.Id, queryID, true);
            Query query = new Query(campaign, queryID);

            conn = new SqlConnection(campaign.ConnString);
            conn.Open();
            string        queryStr = reformatQuery(query.QueryCondition);
            SqlCommand    cmd      = new SqlCommand(queryStr, conn);
            SqlDataReader sdr      = cmd.ExecuteReader();
            string        csv      = "";

            if (dmo.ShowCSVHeaders == 1)
            {
                csv = GetCSVColumnHeaders(sdr);
            }
            while (sdr.Read())
            {
                csv += GetCSVRow(sdr);
            }
            conn.Close();
            return(csv);
        }