using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.WebControls;
using System.Data.OleDb;

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

        }

        protected void RadComboBox1_ItemsRequested(object o, Telerik.WebControls.RadComboBoxItemsRequestedEventArgs e)
        {
            if (e.Text.Length < 1)
                return;

            DataTable dt = FetchMatchingCountries(e.Text);

            RadComboBox combo = (RadComboBox)o;
            combo.Items.Clear();

            foreach (DataRow row in dt.Rows)
            {
                RadComboBoxItem item = new RadComboBoxItem(row["Country"].ToString().ToLower());
                item.Value = row["Code"].ToString();
                combo.Items.Add(item);
            }
        }

        private DataTable FetchMatchingCountries(string partialName)
        {
            string mdbPath = Server.MapPath("~/Combobox.mdb");
            OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath);
            dbCon.Open();

            string sql = "SELECT * from CountryCodes WHERE Country LIKE '" + partialName.Replace("'", "''") + "%'";

            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, dbCon);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            dbCon.Close();

            return dt;
        }

    }
}
