Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                string typedString = txtSearch.Text;
                List<menu> autoList = new List<menu>();
                autoList.Clear();

                foreach (menu item in MenuList)
                {                     
 if(item.Name.ToString().ToLower().StartsWith(typedString.ToString().ToLower()))
                    {
                        autoList.Add(item);
                    }
                }

                if (autoList.Count > 0)
                {
                    lbSuggestion.ItemsSource = autoList;
                    lbSuggestion.Visibility = Visibility.Visible;
                }
                else if (txtSearch.Text.Equals(""))
                {
                    lbSuggestion.Visibility = Visibility.Collapsed;
                    lbSuggestion.ItemsSource = null;
                }
                else
                {
                    lbSuggestion.Visibility = Visibility.Collapsed;
                    lbSuggestion.ItemsSource = null;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }








public class menu
{
    public static string id;
    public static string name;
    public static string price;
    public menu(string id, string name, string price)
    {
        this.Id = id;
        this.Name = name;
        this.Price = price;

    }
    public string Id
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string Price
    {
        get { return price; }
        set { price = value; }
    }

}


What I have tried:

List<menu> MenuList;





MenuList = new List<menu>();
           DataSet ds = Globalvariables.Globals.select_load("Select menu_id,foodname,rprice from add_food_menu where foodname like '%" + txtSearch.Text + "%'");
           if (ds.Tables[0].Rows.Count > 0)
           {
               for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
               {
        string id = ds.Tables[0].Rows[i][0].ToString();
                   string name = ds.Tables[0].Rows[i][1].ToString();
                   string price = ds.Tables[0].Rows[i][2].ToString();
                   MenuList.Add(new menu(ds.Tables[0].Rows[i][0].ToString(),ds.Tables[0].Rows[i][1].ToString(),ds.Tables[0].Rows[i][2].ToString()));

               }

           }
Posted
Comments
Richard MacCutchan 16-Nov-22 9:13am    
What is the question?
prashanth manoj 17-Nov-22 0:47am    
how to retrieve all rows from table in wpf c#
Richard Deeming 17-Nov-22 3:53am    
"Select menu_id,foodname,rprice from add_food_menu where foodname like '%" + txtSearch.Text + "%'"

Not like that! Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

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