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;
   }
}
comments powered by Disqus

Featured

  • VS Code 1.125 Adds Copilot Spend Meter After Billing Shock

    VS Code 1.125 adds in-editor visibility into additional Copilot budget usage as GitHub's AI-credit billing model continues to draw developer scrutiny.

  • TypeScript 7.0 RC Moves Microsoft's Go Rewrite Into the Mainline Compiler

    Microsoft's Go-based TypeScript rewrite has reached Release Candidate status, moving from a separate native-preview package into the regular TypeScript npm package while leaving some ecosystem-facing API work for TypeScript 7.1 or later.

  • Microsoft Highlights Visual Studio Live! Event Lineup and Longtime Developer Community Role

    A Microsoft MVP Blog post on Visual Studio Live!'s longevity arrives as the 2026 conference series continues with upcoming stops at Microsoft HQ, San Diego and Orlando.

  • Using Local AI to Cut Copilot Usage-Based Billing Shock

    After being gobsmacked by the new billing plan using almost all my monthly credits in one or two days, I tried pushing some Copilot-style coding work onto local models in VS Code. What I found was less "free AI" and more "pick your pain": cloud charges on one side, heavy local resource use and long waits on the other.

Subscribe on YouTube