using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using Telerik.Web.UI;

/// <summary>
/// Summary description for LoadEntries
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class LoadEntries : System.Web.Services.WebService
{
    public LoadEntries()
    {

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

    [WebMethod]
    public AutoCompleteBoxData GetCompanyNames(RadAutoCompleteContext context)
    {
        //Accesses the additional data sent from the client.
        string clientData = context["ClientData"].ToString();

        string sql = "SELECT  * from Customers WHERE CompanyName LIKE '" + context.Text + "%'";
        SqlDataAdapter adapter = new SqlDataAdapter(sql,
            ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        DataTable data = new DataTable();
        adapter.Fill(data);
        List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();
        AutoCompleteBoxData dropDownData = new AutoCompleteBoxData();

        result = new List<AutoCompleteBoxItemData>();

        for (int i = 0; i < data.Rows.Count; i++)
        {
            AutoCompleteBoxItemData itemData = new AutoCompleteBoxItemData();
            itemData.Text = data.Rows[i]["CompanyName"].ToString();
            itemData.Value = data.Rows[i]["CustomerID"].ToString();

            result.Add(itemData);
        }

        dropDownData.Items = result.ToArray();
        return dropDownData;
    }
}
