Tuesday, October 23, 2007

Ajax (3.1)


<%@ WebService Language="C#" Class="AspNetAjaxOverview.AutoCompleteService" %>

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data;

using System.Data.SqlClient;

using System.Collections.Generic;

using System.Web.Script.Services;

using System.Configuration;

namespace AspNetAjaxOverview

{

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ScriptService]

public class AutoCompleteService : System.Web.Services.WebService

{

[WebMethod]

[ScriptMethod]

public string[] GetSearchTerms(string prefixText, int count)

{

SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SimpleListsConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand(

"SELECT DISTINCT TOP(@nrows) Name FROM Lists WHERE Name like @term", cn);

cmd.Parameters.AddWithValue("nrows", count);

cmd.Parameters.AddWithValue("term", prefixText + "%");

List<string> suggestions = new List<string>();

cn.Open();

using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))

{

while (dr.Read())

suggestions.Add(dr[0].ToString());

}

return suggestions.ToArray();

}

}

}

blog comments powered by Disqus