0

I'm trying to display name suggestions on a textbox but it's not suggesting anything. I followed a couple tutorials about this but still with the exact same code I cant make it work. I'm using Dapper so maybe I have done something wrong there. Am I missing something?

This is what I have done with Dapper:

public static List<string> DevolverNombres()
        {
            var dbCon = DBConnection.Instancia();
            if (dbCon.Conectado())
            { 
                using (IDbConnection conexion = dbCon.Conexion)
                {
                    var output = conexion.Query($"SELECT nombre FROM usuario;").ToList();

                    var lista = new List<string>();

                    foreach (IDictionary<string, object> row in output) 
                    {
                        foreach (var pair in row)
                        {
                            lista.Add(pair.Value.ToString());
                        }
                    }
                    return lista;
                }
            }
            else return null;
        }

And this is what I have in the form:

private void Home_Load(object sender, EventArgs e) {

            var nombres = AccesoDatos.DevolverNombres();
            var lista = new AutoCompleteStringCollection();

            foreach(string elem in nombres)
            {
                lista.Add(elem);
            }
            txtBuscar.AutoCompleteCustomSource = lista;
        }
Aldimir
  • 193
  • 1
  • 2
  • 16
  • Did you set the property _AutoCompleteSource_ for the textbox to _AutoCompleteSource.CustomSource_? – Steve May 18 '19 at 19:52
  • You're missing some settings in relation to the AutomComplete requirements. You could also use a DataTable instead of a DataReader to load your data. See the description [here](https://stackoverflow.com/a/53249271/7444103) and an implementation using a DataTable [here](https://stackoverflow.com/a/55558178/7444103). – Jimi May 18 '19 at 19:53
  • Did you configure your control as mentioned in https://stackoverflow.com/a/18216863/967736 ? Are your sure that your `DevolverNombres()` method is returning a list with the nombres? – IPValverde May 18 '19 at 19:54
  • If you are just retrieving a list of strings then Dapper allows you to call the Query method with _var output = conexion.Query($"SELECT nombre FROM usuario;");_ and you get directly your list of strings. No need to loop over the IDictionary – Steve May 18 '19 at 20:05
  • Thanks for the information Steve! And I already fixed it, The CustomSource property changed to None dont know why – Aldimir May 19 '19 at 14:03

0 Answers0