Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable dt = new DataTable();

     public string GetCountries(string filter)
     {

         string[] countriesArray = new string[dt.Rows.Count];
         for (int i = 0; i < dt.Rows.Count; i++)
         {
             countriesArray[i] = dt.Rows[i]["country"].ToString();
         }

         var query =
             from c in
                 countriesArray.Select((item, index) => new { id = index, name = item })
                 .Where(p => p.name.IndexOf(filter, StringComparison.InvariantCultureIgnoreCase) >= 0)
             select new { id = Convert.ToString(c.id), name = c.name };


         return new JavaScriptSerializer().Serialize(query);
     }

     protected void Page_Load(object sender, EventArgs e)
     {
         da.SelectCommand = new SqlCommand("select * from Country", cn);
         da.Fill(dt);

         //Check if logged in
         string filter = this.Request.QueryString["q"];
         if(String.IsNullOrEmpty(filter))
             this.Response.Redirect("~/Default.aspx");

         string list = this.GetCountries(filter);
         this.Response.Clear();
         this.Response.ContentType = "text/plain";
         this.Response.Write(list);
         this.Response.End();
     }

the query variable in quick watch display like this:
{id=0,name=Canada}
{id=1,name=USA}

where id is the index of array
how can replace the index with the id of the country in the database ?

note : the dt (datatable) contain two columns id and country
Posted
Updated 4-Jun-15 1:16am
v2

1 solution

Let SQL do the filtering for you:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string filter = Request.QueryString["q"];
        if (string.IsNullOrEmpty(filter))
        {
            Response.Redirect("~/Default.aspx");
            return;
        }
        
        DataTable list = GetCountries(filter);
        string json = SerializeCountries(list);
        
        Response.Clear();
        Response.ContentType = "text/plain"; // This should really be "application/json" or "application/javascript"
        Response.Write(json);
        Response.End();
    }
}

private DataTable GetCountries(string filter)
{
    using (var connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString))
    using (var command = new SqlCommand("SELECT id, country FROM Country WHERE country Like @filter", connection))
    {
        command.Parameters.AddWithValue("@filter", "%" + filter + "%");
        
        var table = new DataTable();
        var da = new SqlDataAdapter(command);
        da.Fill(table);
        return table;
    }
}

private string SerializeCountries(DataTable countries)
{
    var query = from DataRow row in countries.Rows
                select new
                {
                    id = Convert.ToString(row["id"]),
                    name = Convert.ToString(row["country"])
                };
    
    return new JavaScriptSerializer().Serialize(query);
}
 
Share this answer
 
Comments
tranesyasd 7-Jun-15 6:39am    
thank you very much
it is working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900