The source code of the ASP.NET page IndexServer.aspx:
Listing 2
<%@ Page language="c#" Codebehind="IndexServer.aspx.cs" AutoEventWireup="false"
Inherits=" ASPAlliance.IndexServer" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Indexing Service Test</title>
<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="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="LabelNumberOfResults" runat="server"></asp:Label>
<p><asp:DataGrid id="DataGridSearchResults" runat="server"
HeaderStyle-Font-Bold="True" AutoGenerateColumns="False"
BorderWidth="0" AllowCustomPaging="True">
<Columns>
<asp:BoundColumn DataField="RankImageHTML"
HeaderText="Rank" />
<asp:HyperLinkColumn DataTextField="DocumentTitle"
DataNavigateUrlField="DocumentURL"
HeaderText="Document Title" />
<asp:BoundColumn DataField="DocumentDate"
HeaderText="Last Updated" />
</Columns>
</asp:DataGrid></p>
</form>
</body>
</HTML>
The source code of the code behind page IndexServer.aspx.cs:
Listing 3
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace ASPAlliance
{
/// <summary>
/// Summary description for IndexServer.
/// </summary>
public class IndexServer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGridSearchResults;
protected System.Web.UI.WebControls.Label LabelNumberOfResults;
private void Page_Load(object sender, System.EventArgs e)
{
//The search string
string QueryText = "asp alliance";
//The name of your Index Server catalog
string CatalogName = "searchcatalog";
int NumberOfSearchResults = 0;
DataSet SearchResults = new DataSet();
//Prevent SQL injection attacks - further security
//measures are recommended
QueryText = QueryText.Replace("'", "''");
//Build the search query
string SQL = "SELECT doctitle, vpath, Path, Write, Size, Rank";
SQL += "FROM \"" + CatalogName + "\"..SCOPE() ";
SQL += "WHERE";
SQL += " CONTAINS(Contents, '" + QueryText + "') ";
SQL += "AND NOT CONTAINS(Path, '\"_vti_\"') ";
SQL += "AND NOT CONTAINS(FileName, '\".ascx\" OR";
SQL += " \".config\" OR \".css\"') ";
SQL += "ORDER BY Rank DESC";
//Connect to Index Server and perform search query
try
{
OleDbConnection IndexServerConnection = new
OleDbConnection("Provider=msidxs;");
OleDbCommand dbCommand = new
OleDbCommand(SQL, IndexServerConnection);
OleDbDataAdapter IndexServerDataAdapter = new
OleDbDataAdapter();
IndexServerDataAdapter.SelectCommand = dbCommand;
IndexServerDataAdapter.Fill(SearchResults);
NumberOfSearchResults = SearchResults.Tables[0].Rows.Count;
}
catch (Exception ExceptionObject)
{
//Query failed so display an error message
NumberOfSearchResults = 0;
LabelNumberOfResults.Text =
"Problem with retrieving search results due to: " +
ExceptionObject.Message;
DataGridSearchResults.Visible = false;
}
//Build a datatable for search results
DataTable SearchResultsTable = new DataTable("SearchResults");
SearchResultsTable.Columns.Add(new
DataColumn("DocumentRank", typeof(int)));
SearchResultsTable.Columns.Add(new
DataColumn("RankPercentage", typeof(int)));
SearchResultsTable.Columns.Add(new
DataColumn("RankNumber", typeof(int)));
SearchResultsTable.Columns.Add(new
DataColumn("RankImageHTML", typeof(String)));
SearchResultsTable.Columns.Add(new
DataColumn("DocumentURL", typeof(String)));
SearchResultsTable.Columns.Add(new
DataColumn("DocumentTitle", typeof(String)));
SearchResultsTable.Columns.Add(new
DataColumn("DocumentDate", typeof(String)));
if (NumberOfSearchResults > 0)
{
LabelNumberOfResults.Text = NumberOfSearchResults +
" document(s) were found matching the query '" +
QueryText + "'";
foreach (DataRow SearchResultDataRow in
SearchResults.Tables[0].Rows)
{
//Determine the document's date
DateTime DocumentDate;
try
{
DocumentDate = Convert.ToDateTime(
SearchResultDataRow["Write"].ToString());
}
catch
{
DocumentDate = DateTime.Now;
}
//Determine the document's title
string DocumentTitle =
SearchResultDataRow["doctitle"].ToString();
if (DocumentTitle.Length < 2)
{
DocumentTitle = "untitled document";
}
//Determine the document's rank
int RankNumber = 0;
int DocumentRank = Convert.ToInt32(
SearchResultDataRow["Rank"].ToString());
int RankPercentage = DocumentRank;
RankPercentage = Convert.ToInt32(Math.Round(
Convert.ToDouble(RankPercentage / 10), 0));
if (DocumentRank > 900)
{
RankNumber = 10;
}
else if (DocumentRank > 800)
{
RankNumber = 9;
}
else if (DocumentRank > 700)
{
RankNumber = 8;
}
else if (DocumentRank > 600)
{
RankNumber = 7;
}
else if (DocumentRank > 500)
{
RankNumber = 6;
}
else if (DocumentRank > 400)
{
RankNumber = 5;
}
else if (DocumentRank > 300)
{
RankNumber = 4;
}
else if (DocumentRank > 200)
{
RankNumber = 3;
}
else if (DocumentRank > 100)
{
RankNumber = 2;
}
else if (DocumentRank > 0)
{
RankNumber = 1;
}
else
{
RankNumber = 1;
}
string RankingAltTag = RankPercentage + " percent match";
string RankImageHTML = "<img src=\"images/" +
RankNumber +
"bars.png\" width=\"80\" height=\"17\" alt=\"" +
RankingAltTag + "\"> ";
//Populate a DataRow for the current search result
DataRow SearchResultsRow = SearchResultsTable.NewRow();
SearchResultsRow["DocumentRank"] =
SearchResultDataRow["Rank"].ToString();
SearchResultsRow["RankPercentage"] = RankPercentage;
SearchResultsRow["RankNumber"] = RankNumber;
SearchResultsRow["DocumentURL"] =
SearchResultDataRow["vpath"].ToString();
SearchResultsRow["DocumentTitle"] = DocumentTitle;
SearchResultsRow["RankImageHTML"] = RankImageHTML;
SearchResultsRow["DocumentDate"] =
DocumentDate.ToString("dd MMMM yyyy");
SearchResultsTable.Rows.Add(SearchResultsRow);
}
//Bind DataTable to DataGrid
DataGridSearchResults.Visible = true;
DataGridSearchResults.DataSource = SearchResultsTable;
DataGridSearchResults.DataBind();
}
else
{
//No search results were found for the query
DataGridSearchResults.Visible = false;
LabelNumberOfResults.Text =
"No document(s) were found matching the query '" +
QueryText + "'";
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}