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

using Telerik.Web.UI;

[ScriptService]
public class Territories : WebService
{
    [WebMethod]
    public RadComboBoxItemData[] GetTerritories(RadComboBoxContext context)
    {
        SqlConnection connection =
            new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);

        SqlCommand selectCommand =
            new SqlCommand(@"    SELECT [TerritoryID], [TerritoryDescription] FROM [Territories]
                                 WHERE LOWER ([TerritoryDescription]) LIKE @text + '%' ORDER BY [TerritoryDescription]", connection);

        SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
        adapter.SelectCommand.Parameters.AddWithValue("@text", context.Text.ToLower());
        DataTable products = new DataTable();
        adapter.Fill(products);

        List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(products.Rows.Count);

        foreach (DataRow row in products.Rows)
        {
            RadComboBoxItemData itemData = new RadComboBoxItemData();

            itemData.Text = row["TerritoryDescription"].ToString();
            itemData.Value = row["TerritoryID"].ToString();

            result.Add(itemData);
        }

        return result.ToArray();
    }
}