How to use an ASP.NET application to query an Indexing Service catalog by using Visual C# .NET (820983)
The information in this article applies to:
- Microsoft ASP.NET (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
- Indexing Service
For a Microsoft Visual Basic .NET version of this article, see 820105.
SUMMARYThis step-by-step article describes how to create an
Indexing Service catalog. This article also describes how to use an ASP.NET Web
application to query Indexing Service catalogs. back to the topCreate a test folder- Create a new folder on the C drive of your computer. Name
the folder myCatalogFolder.
- Start a text editor such as Notepad, and then paste the following text in a blank
document:
This is test document to test the index server query and this file name is IndexText.text. - Save the file as
C:\myCatalogFolder\IndexText.txt.
back to the
top Create an Indexing Service catalog- Click Start, click Run, type compmgmt.msc, and then click OK.
- Expand Services and Applications.
- Right-click Indexing Service, point to
New, and then click Catalog.
- In the Name text box, type
TestCatalog.
- Click Browse, locate the folder where you
want to put the catalog, and then click OK two times.
- Under New Catalog
Created, you receive the following message: Catalog will remain off-line until Indexing Service is restarted Click OK.
- Right-click Indexing Service, and then
click Stop to stop the Indexing Service.
- Right-click Indexing Service, and then
click Start to restart the Indexing Service.
back to the
top Define the scope of the catalog After you create a new catalog, add the folders that you want
to include in the scope of the catalog. The scope is the set of folders that is
included in the catalog and excluded from the catalog. The scope defines the content that
is included in the index and excluded from the index. For each folder that is included or
excluded, all its subfolders are also included or excluded. - Double-click TestCatalog (the catalog that
you created in the "Create a Test Folder" section).
- Right-click Directories, point to
New, and then click Directory.
- Click Browse, locate and then click the
folder that you want to add (C:\myCatalogFolder\), and then click OK.
Note Under Include in Index ?, click
Yes. back to the
topCreate an ASP.NET Web application- Start Microsoft Visual Studio .NET.
- Use Visual C# .NET to create a new ASP.NET Web
Application project. Name the project IndexQueryApp.
By default, WebForm1.aspx is created.
- In Design view, right-click
WebForm1, and then click View HTML Source.
- Replace the existing HTML code with following sample
code:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="IndexQueryApp.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm3" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 90px; POSITION: absolute; TOP: 136px" runat="server"></asp:TextBox>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 328px; POSITION: absolute; TOP: 138px" runat="server"></asp:DataGrid>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 92px; POSITION: absolute; TOP: 199px" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML> - In Solution Explorer, right-click
WebForm1, and then click View Code. Replace
the existing code with the following sample code:
using System;
using System.Data;
namespace IndexQueryApp
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here.
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: The ASP.NET Web Form Designer requires this call.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method by using the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
// Catalog Name
string strCatalog = "TestCatalog";
string strQuery="";
strQuery = "Select DocTitle,Filename,Size,PATH,URL from Scope() where FREETEXT('" +TextBox1.Text+ "')";
// TextBox1.Text is the word that you type in the text box to query by using Indexing Service.
string connstring = "Provider=MSIDXS.1;Integrated Security .='';Data Source="+strCatalog;
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connstring);
conn.Open();
System.Data.OleDb.OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataSet testDataSet = new System.Data.DataSet();
cmd.Fill(testDataSet, "SearchResults");
DataView source = new DataView(testDataSet.Tables[0]);
DataGrid1.DataSource = source;
DataGrid1.DataBind();
}
}
}
- On the Build menu, click Build
Solution.
back to the
topRun the application- On the Debug menu, click
Start to run the application.
- Type the query word document in the text
box. (You can type any word from the IndexText.txt file.)
- Click Button. Notice that the data grid
appears and contains the query results from the IndexText.txt
file.
back to the
topTroubleshootWait for the index to be builtIndexing Service automatically processes the TestCatalog
and prepares a word list for indexing. When you run the sample code, you may
not receive the results as quickly as expected because Indexing Service requires some time
to build the index. To verify the state of the index, start the Computer Management MMC snap-in. Click Indexing
Service in the right pane, and then verify that the Word
List value for TestCatalog is greater than zero. If the Word
List value is zero, wait for the index to build the list. Index data may have become corruptedIf the search does not return the expected search results
when you query the index, the index data may have become corrupted. To
troubleshoot this issue, stop and then restart Indexing Service to index all
documents again. To do this, follow these steps:
- Click Start, point to
Settings, and then click Control Panel.
- Double-click Administrative Tools, and
then double-click Computer Management.
- In the console tree, double-click Services and
Applications.
- Right-click Indexing Service, and then
click Stop.
- Right-click Indexing Service again, and then
click Start.
back to the
topREFERENCESFor more information about the Indexing Service, visit the
following Microsoft Web site:
For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
311521
How to index ASP.NET content by using Microsoft Index Server
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
308202
How to create and configure a catalog for indexing
back to the
top
Modification Type: | Minor | Last Reviewed: | 6/30/2006 |
---|
Keywords: | kbQuery kbService kbDataBinding kbDataAdapter kbWebServer kbWebForms kbHOWTOmaster KB820983 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|