//
you're reading...
AjaxControlToolKit, Asp.net, AutoComplete

AutoComplete From Database Using AutoCompleteExtender

To Create an AutoComplete TextBox in asp.net use following instructions:

1. Create a webmethod GetDivisionInfo


public string[] GetDivisionInfo(string prefixText)
{
string connStr = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
int count = 10;
string sql = "Select * from Division Where vDivisionName like @prefixText";
SqlDataAdapter da = new SqlDataAdapter(sql,connStr);
da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText+ "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["vDivisionName"].ToString(),i);
i++;
}
return items;
}

Replace connStr with your existing connection string.


2. Drag and Drop a TextBox from ToolBar and set the AutoCompleteExtender’s TargetControlID property to the TextBox Id. Set ServicePath as WebService.asmx, ServiceMethod as GetDivisionInfo and MinimimPrefixLength as 1.

<asp:TextBox ID="txtDivision" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="txtDivision_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServicePath="WebService.asmx" ServiceMethod="GetDivisionInfo"
TargetControlID="txtDivision" MinimumPrefixLength="1">
</cc1:AutoCompleteExtender>

Advertisement

About Anupam

Software Developer @KDS Group

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Archives

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1 other follower

Follow

Get every new post delivered to your Inbox.