Ajax auto complete textbox code

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<
br />
<asp:Label ID="Label1" runat="server" BackColor="#FFCCFF" ForeColor="#003300"
Height="23px" Text="Products" Width="8%"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled"
Height="27px" Width="236px"></asp:TextBox>
<
asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" MinimumPrefixLength="1"
ServiceMethod="GetCompletionList" ServicePath=""
TargetControlID="TextBox1" UseContextKey="True">
</
asp:AutoCompleteExtender
>

.cs code

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
string cs = ConfigurationManager.ConnectionStrings["NorthwindCS"].ConnectionString;
SqlConnection cn = new SqlConnection(cs);
SqlDataAdapter da=new SqlDataAdapter("select ProductName from Products",cn);
DataSet ds = new DataSet();
da.Fill(ds,
"Products");
DataView dv = new DataView(ds.Tables[0]);
dv.RowFilter =
string.Format("ProductName like '{0}%'",prefixText);
int rcount, size;
rcount = dv.Count;
if (rcount >= count)
size = count;
else
size = rcount;
string[] Pnames = new string[size];
for (int i = 0; i < size; i++ )
Pnames[i] = dv[i][
"ProductName"].ToString();
return Pnames;
}

3 Comments

Join the conversation! Your thoughts help the community grow.

  • Pravas Ranjan

    Hello Sandeep please elaborate more when you posting the article not only code so it will be better for reader to understand conceptually.....

  • Pravas Ranjan

    Sandeep Bhai i am very much glad to see you here...good article Sandy...keep it up and keep posting more article like this......

  • Vithal Wadje

    where is web service..?