Hi
try this example to make JQuery AutoComplete from Database
Step1 :-
First download the following files jquery.autocomplete.js and jquery.autocomplete.css
Step2 :-
Open VS 2005 and create new website and add new Web page and Web.Config and WebHandler with name “AutocompleteData.ashx”
Step3 :-
In AutocompleteData.ashx add the following code :-
<%@ WebHandler Language="C#" >
using System;
using System.Web;
using System.Data.SqlClient;
public class AutocompleteData : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string firstname = context.Request.QueryString["q"];
string sql = "select top 10 FirstName from Employees where FirstName like '" + firstname + "%'";
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
context.Response.Write(reader.GetString(0) + Environment.NewLine);
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
Note :- i am using Northwind Database in this example
Step4 :-
Open Default.aspx page and Dragr Textbox and change Id = “txt_Search”
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script src="jquery.js" type="text/javascript"></script>
<link rel="stylesheet" href="jquery.autocomplete.css" type="text/css" />
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#txt_Search").autocomplete("AutocompleteData.ashx"); });
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Search :- <asp:TextBox ID="txt_Search" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
and then run your Web page
Hope this helps
Good Lucl.