Step
1 :- AutoFill.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoFill.aspx.cs" Inherits="Demo.AutoFill" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ScriptManager1" runat="server"></asp:ToolkitScriptManager>
<div>
<b>Searching
Name in Database Record</b>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="TextBox2" ServiceMethod="FillTextBoxFromDatabase" MinimumPrefixLength="1" CompletionInterval="10" UseContextKey="True"></asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>
Step 2 :- AutoFill.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class AutoFill :
System.Web.UI.Page
{
static SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
[WebMethod]
public static List<string>
FillTextBoxFromDatabase(string prefixText)
{
List<string>
listRegionName = new List<string>();
con = new SqlConnection("server=Server
Name; database=Database Name; uid=User ID; pwd=Password");
SqlCommand cmd
= new SqlCommand();
cmd.CommandText = "select RegionID, RegionName from IC_RegionMaster
where RegionName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText",
prefixText);
cmd.Connection = con;
con.Open();
SqlDataReader dr =
cmd.ExecuteReader();
while (dr.Read())
{
listRegionName.Add(dr["RegionName"].ToString());
}
con.Close();
return listRegionName;
}
}
}
Note
:-
Download AjaxControlToolkit.dll