using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Collections.Generic;
using System.Web.Services;
using System.Data.SqlClient;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [WebMethod]
    public static AutoCompleteBoxData GetCompanyNames(object context)
    {
        string searchString = ((Dictionary<string, object>)context)["Text"].ToString();
        DataTable data = GetChildNodes(searchString);
        List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();

        foreach (DataRow row in data.Rows)
        {
            AutoCompleteBoxItemData childNode = new AutoCompleteBoxItemData();
            childNode.Text = row["ProductName"].ToString();
            childNode.Value = row["ProductID"].ToString();
            result.Add(childNode);
        }

        AutoCompleteBoxData res = new AutoCompleteBoxData();
        res.Items = result.ToArray();

        return res;
    }

    private static DataTable GetChildNodes(string searchString)
    {
        SqlCommand selectCommand = new SqlCommand(@"SELECT * FROM [Products] WHERE ProductName LIKE @ProductName + '%'");
        selectCommand.Parameters.AddWithValue("ProductName", searchString.Replace("%", "[%]").Replace("_", "[_]"));
        return GetData(selectCommand);
    }

    private static DataTable GetData(SqlCommand selectCommand)
    {
        selectCommand.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);

        DataTable data = new DataTable();
        adapter.Fill(data);

        return data;
    }
}
