using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Data.SqlClient;
using System.Configuration;


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

    public AutoComplete () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    

    [WebMethod]
    [ScriptMethod]
    public string[] GetCompletionListOfCustomers(string prefixText, int count)
    {
        List<string> list = new List<string>();
        //list.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem("Vins et alcools Chevalier", "10248"));        
        
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Select CustomerID as value, ContactName as text from Customers Where ContactName Like @KeyWord + '%'", con);
        cmd.Parameters.Add(new SqlParameter("@KeyWord", prefixText));
        try
        {
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();            
            while (dr.Read())
            {                
                list.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dr["text"].ToString(), dr["value"].ToString()));
            }
        }
        finally
        {
            con.Close();
        }

        return list.ToArray();
    }
}

