Karthik, if you could email me I'll send you back the compiled code and some instructions.
For the really geeky among you (that means everyone![:D]) here is the code. I didn't come up with the ajax part, I got that from the Code Project, so here's the code to implement it.
This is the web page
<%@ Page language="c#" Codebehind="ProductSearchForm.aspx.cs" AutoEventWireup="false" Inherits="karthikweb1.ProductSearchForm" %>
<%@ Register TagPrefix="wcp" Namespace="WCPierce.Web.UI.WebControls" Assembly="WCPierce.Web" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href="StyleSheet1.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:Label id="InstructionLabel" runat="server">Please type the product to search for here</asp:Label></P>
<table border="0">
<tr>
<td>
<wcp:AutoCompleteTextBox runat="server" id="ProductTxt" OnTextChanged="ProductTxt_TextChanged" ListItemCssClass="ListItem"
ListItemHoverCssClass="ListItemHover" />
<asp:Button id="Button1" runat="server" Text="Button" />
</td>
</tr>
</table>
<asp:Label id="TraceLabel" runat="server" Visible="False"></asp:Label>
<P>
<asp:Label id="ResultsLabel" runat="server"></asp:Label></P>
</form>
</body>
</HTML>
This is the code behind for the above page
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
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.Configuration;
using WCPierce.Web;
namespace karthikweb1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class ProductSearchForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label InstructionLabel;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label ResultsLabel;
protected WCPierce.Web.UI.WebControls.AutoCompleteTextBox ProductTxt;
private string searchterm;
protected System.Web.UI.WebControls.Label TraceLabel;
private string connstring;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
connstring = ConfigurationSettings.AppSettings["connString"];
}
protected void ProductTxt_TextChanged(object s, EventArgs e)
{
Searching search = new Searching( connstring );
search.OrgforAjax(s);
}
#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.ProductTxt.TextChanged += new System.EventHandler(this.ProductTxt_TextChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//this.ProductTxt.TextChanged += new System.EventHandler(this.ProductTxt_TextChanged);
private void Button1_Click(object sender, System.EventArgs e)
{
decimal price = 0;
searchterm = ProductTxt.Text;
string proc = "select price FROM Products WHERE productname = '" + searchterm + "';";
using ( OleDbConnection conn = new OleDbConnection (connstring) )
{
using ( OleDbCommand cmd = new OleDbCommand( proc, conn ) )
{
conn.Open();
using ( OleDbDataReader dr = cmd.ExecuteReader() )
{
while ( dr.Read() )
{
price = (decimal) dr[0];
}
dr.Close();
ResultsLabel.Text = "Product: " + searchterm + " Price: " + price.ToString( "c" );
}//end using dr
}//end using cmd
}//end using conn
}//end Button1_Click
}//end class
}//end namespace
and this is the Searching class
using System;
using System.Data.OleDb;
using WCPierce;
using WCPierce.Web;
using WCPierce.Web.UI.WebControls;
namespace karthikweb1
{
/// <summary>
/// Summary description for Searching.
/// </summary>
public class Searching
{
private OleDbConnection conn = null;
public Searching(string conn)
{
this.conn = new OleDbConnection(conn);
}
public void OrgforAjax(object s)
{
OleDbDataReader dr = null;
try
{
AutoCompleteTextBox act = s as AutoCompleteTextBox;
string searchterm = act.Text;
int length = searchterm.Length;
string proc = "select ProductName from Products where LEFT(ProductName," + length.ToString() + " ) = '" + searchterm + "' ORDER BY ProductName";
OleDbCommand cmd = new OleDbCommand(proc, conn);
conn.Open();
dr = cmd.ExecuteReader();
act.DataSource = dr;
act.DataTextField = "ProductName";
act.BindData();
}//end of try
catch(Exception ex)
{
CallBackHelper.HandleError( ex );
}//end of catch
finally
{
dr.Close();
conn.Close();
}//end of finally
}//end of OrgforAjax
}//end of class
}//end of namespace
oops! Almost forgot the web.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- PUT YOUR CONNSTRING HERE IN THE VALUE ATTRIBUTE!!! -->
<add key="connString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\docs\db3.mdb"/>
<add key="AutoCompleteTextBox.ScriptPath" value="~\scripts\AutoCompleteTextBox.js" />
</appSettings>
<system.web>
<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to enable ASPX debugging. Otherwise, setting this value to
false will improve runtime performance of this application.
Set compilation debug="true" to insert debugging symbols (.pdb information)
into the compiled page. Because this creates a larger file that executes
more slowly, you should set this value to true only when debugging and to
false at all other times. For more information, refer to the documentation about
debugging ASP.NET files.
-->
<compilation
defaultLanguage="c#"
debug="true"
/>
<!-- CUSTOM ERROR MESSAGES
Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable.
Add <error> tags for each of the errors you want to handle.
"On" Always display custom (friendly) messages.
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running
on the local Web server. This setting is recommended for security purposes, so
that you do not display application detail information to remote clients.
-->
<customErrors
mode="RemoteOnly"
/>
<!-- AUTHENTICATION
This section sets the authentication policies of the application. Possible modes are "Windows",
"Forms", "Passport" and "None"
"None" No authentication is performed.
"Windows" IIS performs authentication (Basic, Digest, or Integrated Windows) according to
its settings for the application. Anonymous access must be disabled in IIS.
"Forms" You provide a custom form (Web page) for users to enter their credentials, and then
you authenticate them in your application. A user credential token is stored in a cookie.
"Passport" Authentication is performed via a centralized authentication service provided
by Microsoft that offers a single logon and core profile services for member sites.
-->
<authentication mode="Windows" />
<!-- AUTHORIZATION
This section sets the authorization policies of the application. You can allow or deny access
to application resources by user or role. Wildcards: "*" mean everyone, "?" means anonymous
(unauthenticated) users.
-->
<authorization>
<allow users="*" /> <!-- Allow all users -->
<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>
<!-- APPLICATION-LEVEL TRACE LOGGING
Application-level tracing enables trace log output for every page within an application.
Set trace enabled="true" to enable application trace logging. If pageOutput="true", the
trace information will be displayed at the bottom of each page. Otherwise, you can view the
application trace log by browsing the "trace.axd" page from your web application
root.
-->
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
<!-- SESSION STATE SETTINGS
By default ASP.NET uses cookies to identify which requests belong to a particular session.
If cookies are not available, a session can be tracked by adding a session identifier to the URL.
To disable cookies, set sessionState cookieless="true".
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
<!-- GLOBALIZATION
This section sets the globalization settings of the application.
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>