Monday, March 26, 2012

How do i create autocomplete with database ?

How do i create autocomplete with database like sql words list.. pls, thanks!

Hope this helps:

http://forums.asp.net/thread/1563579.aspx

If noone else answers here, try searching forum... 100s of autocomplete questions and codes..


Have you taken a look at theCascadingDropDown Walkthrough that does something very similar? You can apply the same principles to the autocomplete extender.

What I do is create a local web service that queries the database. An example of the code for the web service is as follows:

using

System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

///

<summary>
/// Summary description for roster_list
///</summary>
[WebService(Namespace =http://tempuri.org/)]
[WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
publicclassroster_list : System.Web.Services.WebService {

public roster_list () {

}

[WebMethod]
publicstring[] GetRosterListValues(string prefixText,int count)
{
SqlConnection RosterListConnection;
SqlCommand RosterListCommand;
SqlDataReader RosterListReader;
DataTable RosterListDataTable =newDataTable();

RosterListConnection =newSqlConnection();
RosterListConnection.ConnectionString =ConfigurationManager.ConnectionStrings["RosterConnectionString"].ConnectionString;
RosterListCommand =newSqlCommand();

RosterListCommand.CommandText ="SET ROWCOUNT @.Count " +
" SELECT column " +
" FROM t_table" +
" WHERE column LIKE @.PrefixText + '%') " +
" ORDER BY column " +
" SET ROWCOUNT 0";

RosterListCommand.Parameters.AddWithValue("@.PrefixText", prefixText);
RosterListCommand.Parameters.AddWithValue("@.Count", count);
RosterListCommand.CommandType =CommandType.Text;
RosterListCommand.Connection = RosterListConnection;

try
{
RosterListCommand.Connection.Open();
RosterListReader = RosterListCommand.ExecuteReader(CommandBehavior.CloseConnection);
RosterListDataTable.Load(RosterListReader);
}
catch { }
finally
{
RosterListDataTable.Dispose();
RosterListCommand.Dispose();
RosterListConnection.Dispose();
}

string[] ReturnValues =newstring[RosterListDataTable.Rows.Count];
for (int i = 0; i < RosterListDataTable.Rows.Count; i++)
ReturnValues[i] = RosterListDataTable.Rows[i][0].ToString();

return ReturnValues;
}
}

Then for my extender I have:

<cc1:AutoCompleteExtenderID="AutoCompleteExtender_Roster"runat="server"
TargetControlID="TextBox_Roster"
CompletionInterval="250"
MinimumPrefixLength="1"
ServicePath="../web_services/roster_list.asmx"
ServiceMethod="GetRosterListValues"
CompletionSetCount="15" />

No comments:

Post a Comment