views:

203

answers:

0

Hi All,

I am trying to add a search feature to my website using IIS indexing .I have 50 + static html pages and one ASP.NET page which is supposed to show the search results. I used the following code.

protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
protected System.Data.OleDb.OleDbDataAdapter dbAdapter1;
OleDbDataAdapter dbAdapter;
protected System.Data.OleDb.OleDbConnection dbConnection;

override protected void OnInit(EventArgs e)
   {
       //
       // CODEGEN: This call is required by the ASP.NET Web Form Designer.
       //
       InitializeComponent();
       base.OnInit(e);
   }
   private void InitializeComponent()
   {
       this.dbAdapter = new System.Data.OleDb.OleDbDataAdapter();
       this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
       this.dbConnection = new System.Data.OleDb.OleDbConnection();
       this.dgResultsGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgResultsGrid_PageIndexChanged);
       // 
       // dbAdapter
       // 
       this.dbAdapter.SelectCommand = this.oleDbSelectCommand1;
       // 
       // oleDbSelectCommand1
       // 
       this.oleDbSelectCommand1.Connection = this.dbConnection;
       // 
       // dbConnection
       // 
     //  this.dbConnection.ConnectionString = "Provider=MSIDXS.1;Integrated Security .=\"\";Data Source=Web";
      this.dbConnection.ConnectionString = "Provider=MSIDXS.1;Integrated Security .=\"\";Data Source=TestSearch";

   }


 private void Search()
{
    // Create a new DataSet and fill it.
    try
    {
        this.dbAdapter.SelectCommand.CommandText = Command;
        DataSet ds = new DataSet("Results");
        this.dbAdapter.Fill(ds);

        this.lblResultCount.ForeColor = Color.Black;
        int rows = ds.Tables[0].Rows.Count;
        this.lblResultCount.Text = String.Format("{0} document{1} found{2}",
            rows, rows == 1 ? " was" : "s were", rows == 0 ? "." : ":");

        // Bind the resulting DataSet.
        this.dgResultsGrid.DataSource = ds;
        this.dgResultsGrid.DataBind();

        // If all was bound well, display the DataGrid.
        this.dgResultsGrid.Visible = (rows > 0);
    }
    catch (Exception ex)
    {
        this.lblResultCount.ForeColor = Color.Red;
        this.lblResultCount.Text = String.Format("Unable to retreive a list " +
            "of documents for the specified query: {0}", ex.Message);

        this.dgResultsGrid.Visible = false;
    }
    finally
    {
        this.lblResultCount.Visible = true;
    }
}
protected object GetTitle(object value)
{
    string title = DataBinder.Eval(value, "DocTitle") as string;
    if (title != null && title.Length > 0) return title;

    return DataBinder.Eval(value, "Filename");
}

  private string Command
  {
    get
    {
        // Construct the base query.
        string text = "Compensation";  // my search keyword
        string query = String.Format(@"
SELECT Rank, VPath, DocTitle, Filename, Characterization, Write
FROM SCOPE('DEEP TRAVERSAL OF ""{0}""')
WHERE NOT CONTAINS(VPath, '""_vti_"" OR "".config"" OR "".txt""')", "/");



        // Conditionally construct the rest of the WHERE clause.
        string type = "any";
       // string fmt = @" AND (filename NOT LIKE '%.doc') AND (filename NOT LIKE '%.txt') AND (filename NOT LIKE '%.js') AND (filename NOT LIKE '%.pdf') AND (filename NOT LIKE '%.ppt') AND (CONTAINS('{0}') OR CONTAINS(DocTitle, '{0}'))";
        string fmt = @" AND (filename NOT LIKE '%.doc') AND (filename NOT LIKE '%.xml') AND (filename NOT LIKE '%.txt') AND (CONTAINS('{0}') OR CONTAINS(DocTitle, '{0}'))";

        // Get the query string and remove all semi-colons, which should stop
        // attempt to run malicious SQL code.

        if (type == "all" || type == "any" || type == "boolean")
        {
            string[] words = text.Split(' ');
            int len = words.Length;
            for (int i = 0; i < len; i++)
            {
                string word = words[i];
                if (type == "boolean")
                    if (String.Compare(word, "and", true) == 0 ||
                        String.Compare(word, "or", true) == 0 ||
                        String.Compare(word, "not", true) == 0 ||
                        String.Compare(word, "near", true) == 0)
                        continue;

                words[i] = String.Format(@"""{0}""", word);
                if (i < len - 1)
                {
                    if (type == "all") words[i] += " AND";
                    else if (type == "any") words[i] += " OR";
                }
            }

            query += String.Format(fmt, String.Join(" ", words));
        }
        else if (type == "exact")
        {
            query += String.Format(fmt, text);
        }
        else if (type == "natural")
        {
            query += String.Format(" AND FREETEXT('{0}')", text);
        }

        // Sort the results.
        query += String.Format(" ORDER BY {0} {1}","Rank","ASC");


        Trace.Write("Query", query);
        return query;
    }
}

I have enabled indexing service in my IIS.Then i created a new catalog called "TestSearch" . and Specified location as the Folder location where i have the files. But when executing the search method. I am not getting any results. But if i replace "TestSearch" with "Web" in this line, this.dbConnection.ConnectionString = "Provider=MSIDXS.1;Integrated Security .=\"\";Data Source=TestSearch"; , Its returning results.But the results are from outside the folder which i have mentioned while creating the catalog.

Can any one guide me how to go ahead ?

Thanks in advance