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

/// <summary>
/// Summary description for TestWebService
/// </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 TestWebService : System.Web.Services.WebService
{

    [WebMethod]
    public RadComboBoxData GetData(RadComboBoxContext context)
    {

        string sql = "SELECT * from Customers WHERE CompanyName LIKE @text + '%'";

        SqlDataAdapter adapter = new SqlDataAdapter(sql,
            ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        DataTable data = new DataTable();

        adapter.SelectCommand.Parameters.AddWithValue("@text", context.Text);
        adapter.Fill(data);

        List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(context.NumberOfItems);
        RadComboBoxData comboData = new RadComboBoxData();
        try
        {

            int itemsPerRequest = 10;
            int itemOffset = context.NumberOfItems;
            int endOffset = itemOffset + itemsPerRequest;
            if (endOffset > data.Rows.Count)
            {
                endOffset = data.Rows.Count;
            }
            if (endOffset == data.Rows.Count)
            {
                comboData.EndOfItems = true;
            }
            else
            {
                comboData.EndOfItems = false;
            }
            result = new List<RadComboBoxItemData>(endOffset - itemOffset);
            for (int i = itemOffset; i < endOffset; i++)
            {
                RadComboBoxItemData itemData = new RadComboBoxItemData();
                itemData.Text = data.Rows[i]["CompanyName"].ToString();
                itemData.Value = data.Rows[i]["CompanyName"].ToString();

                result.Add(itemData);
            }

            if (data.Rows.Count > 0)
            {
                comboData.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), data.Rows.Count.ToString());
            }
            else
            {
                comboData.Message = "No matches";
            }
        }
        catch (Exception e)
        {
            comboData.Message = e.Message;
        }

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



}

