Create Rich Web Apps with AJAX: Listing 4: C#, Fill in the Blank
The AutoCompleteService Web service handles retrieving matching data items from a database based upon characters passed to it in the prefixText parameter. Matching data items are returned from the service using a string array.
[ScriptService]
[WebService(Namespace =
"http://xmlforasp.net/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
public class AutoCompleteService :
System.Web.Services.WebService
{
public AutoCompleteService()
{
//Uncomment the following line if
// using designed components
//InitializeComponent();
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[]
GetSearchItems(string prefixText,
int count)
{
ProductsDB db = new ProductsDB();
SqlDataReader reader =
db.SearchProductDescriptions(
prefixText);
if (reader != null &&
reader.HasRows)
{
int counter = 0;
List<string> items =
new List<string>();
while (reader.Read())
{
if (counter <= count)
{
string model =
reader["ModelName"].ToString();
if (model.Length > 14) model =
model.Substring(0, 14);
items.Add(model);
counter++;
}
else
{
break;
}
}
if (items.Count > 0)
{
return items.ToArray();
}
reader.Close();
}
return null;
}
}