void doSearch(string advanceQry)
        {
            try
            {
                using (HighPrecisionTimer hpt = new HighPrecisionTimer("timer", true))
                {
                    List <DataTable> tables = new List <DataTable>();

                    int limit = Toolkit.ToInt32(lim.SelectedValue, 0);

                    string query = "";
                    if (ml.Checked)
                    {
                        query = q2.Text;
                    }
                    else
                    {
                        query = q.Text;
                    }

                    q.Text = q2.Text = query;

                    string sDv           = "";
                    string advancedQuery = "";
                    if (advanceQry == "")
                    {
                        advancedQuery = getAdvancedQuery();
                    }
                    else
                    {
                        advancedQuery = advanceQry;
                    }
                    var searchString = query;
                    query = query + advancedQuery;
                    string op = ma.Checked ? " and " : " or ";
                    if (query.Length >= op.Length)
                    {
                        if (query.Substring(0, op.Length) == op)
                        {
                            query = query.Substring(op.Length, query.Length - op.Length);
                        }
                    }

                    //if (ed.Checked && query != "")
                    //    query = query + " AND (@inventory.is_distributable = 'Y' and @inventory.is_available = 'Y')";

                    if (query == "")
                    {
                        Master.ShowError(Page.GetDisplayMember("Search", "doSearch{noCriteria}", "Please enter search string or other search criteria and try again."));
                    }
                    else
                    {
                        var searchIndexes = Toolkit.GetSetting("WebSearchableIndexes", "accession, accession_name, taxonomy_species, inventory, accession_source, cooperator");

                        DataSet ds = null;
                        using (var sd = new SecureData(true, UserManager.GetLoginToken(true)))
                        {
                            //ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, limit, 0, 0, view.SelectedValue, "passthru=always;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null);
                            if (exp.Checked)
                            {
                                sDv = "web_search_overview_export";
                            }
                            else
                            {
                                sDv = view.SelectedValue;
                            }

                            //if (ed.Checked)
                            //    ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, 0, 0, 0, "", "passthru=nonindexedorcomparison;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null);
                            //else
                            //    ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, limit, 0, 0, sDv, "passthru=nonindexedorcomparison;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null);
                            query = query + " and @accession.is_web_visible = 'Y' ";
                            query = Utils.Sanitize2(query);

                            ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, limit, 0, 0, sDv, "passthru=nonindexedorcomparison;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null);

                            var dtError = ds.Tables["ExceptionTable"];
                            if (dtError != null && dtError.Rows.Count > 0)
                            {
                                if (dtError.Rows[0]["Message"].ToString().ToLower().StartsWith("bad search string"))
                                {
                                    Master.ShowError(Page.GetDisplayMember("Search", "doSearch{invalidquery}", "Invalid query.  Please alter your criteria and try again."));
                                }
                                else
                                {
                                    var type = dtError.Rows[0]["ExceptionType"].ToString().ToLower();
                                    if (type.Contains("endpointnotfoundexception"))
                                    {
                                        throw new InvalidOperationException(dtError.Rows[0]["Message"].ToString());
                                    }
                                    else
                                    {
                                        Master.ShowError(dtError.Rows[0]["Message"].ToString());
                                    }
                                }
                            }

                            /*
                             * else
                             * {
                             *  if (ed.Checked)
                             *  {
                             *      List<int> IDs = new List<int>();
                             *      int id;
                             *      if (ds != null)
                             *      {
                             *          foreach (DataRow dr in ds.Tables["SearchResult"].Rows)
                             *          {
                             *              if (int.TryParse(dr[0].ToString(), out id))
                             *                  IDs.Add(id);
                             *          }
                             *      }
                             *
                             *      DataTable dtID = null;
                             *      List<int> IDa = new List<int>();
                             *      using (var dm = DataManager.Create(sd.DataConnectionSpec))
                             *      {
                             *          dtID = dm.Read("select distinct accession_id from inventory where is_distributable =  'Y' and is_available = 'Y'");
                             *          if (dtID != null)
                             *          {
                             *              foreach (DataRow dr in dtID.Rows)
                             *              {
                             *                  if (int.TryParse(dr[0].ToString(), out id))
                             *                      IDa.Add(id);
                             *              }
                             *          }
                             *      }
                             *
                             *      List<int> IDavail = IDs.Intersect(IDa).ToList();
                             *
                             *      if (limit < IDavail.Count)
                             *          IDavail = IDavail.GetRange(0, limit);
                             *
                             *      ds = sd.GetData(sDv, ":idlist=" + string.Join(",", IDavail.Select(n => n.ToString()).ToArray()), 0, 0, "");
                             *  }
                             * } */
                        }
                        hpt.Stop();

                        //TimeSpan ts = TimeSpan.FromMilliseconds(hpt.ElapsedMilliseconds);
                        //string elapsed = ts.Seconds.ToString("00") + "." + ts.Milliseconds.ToString("000");

                        // lblTimer.Text = "Search time: " + elapsed + " seconds";

                        DataTable dt = ds.Tables[sDv];
                        if (exp.Checked)
                        {
                            pnlSearchResults.Visible = false;
                            if (dt == null)
                            {
                                Master.ShowError(Page.GetDisplayMember("Search", "doSearch{errorout}", "Query limit exceeded capacity.  Please reduce your return accessions number and try again."));
                            }
                            else
                            {
                                Toolkit.OutputData(dt, Response, "csv", false, true);
                                //exportToExcel(dt);
                            }
                        }
                        else
                        {
                            pnlSearchResults.Visible = true;
                            //if (rs.SelectedValue.Contains("obs")) {
                            //    // special case: obs data is stored row-wise,
                            //    //               we need it column-wise
                            //    // need to pivot by the 'trait_value' column

                            //    dt = dt.transform(new string[] { "accession_id", "pi_number" }, "trait_name", null, "trait_value");

                            //}

                            if (dt == null || dt.Rows.Count == 0)
                            {
                                pnlSearchResultsHeader.Visible = false;
                                ggPivotView.DataSource         = dt;
                                ggPivotView.DataBind();
                            }
                            else
                            {
                                pnlSearchResultsHeader.Visible = true;
                                ggPivotView.DataSource         = dt;
                                ggPivotView.DataBind();
                            }
                            var display = "<b>Query Criteria:</b><br /> " + "&nbsp; &nbsp; &nbsp;Search String: " + searchString + "<br />" + getAdvancedQueryDisplay();
                            Master.ShowMore(display);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                pnlSearch.Visible     = false;
                pnlSearchDown.Visible = true;
                if (Page.User.IsInRole("Admins"))
                {
                    pnlError.Visible = true;
                    SearchError      = ex.ToString();
                }
                else
                {
                    pnlError.Visible = false;
                }
            }
        }