0

I want to create an autocomplete textbox with my database.

I'm programming my application in a layered architecture (models, DAL, BLL, Presentation).

I've already made a method with an arraylist that reads and returns my select command in the database, which is filling (I've tested on a combobox).

But when I try to insert in the the textbox, nothing happens... it doesn't show the suggestion.

I looked for something in the forum but I just found examples with one layer and, since I'm developing in layers I cannot increment the property AutoCompleteStringCollection in my DAL to be filled by my select command.

If anyone has any idea how to solve this problem, please explain to me!

Additional information: I'm using winForm with C# and SQL Server.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128

2 Answers2

0

I think you want to say that "But when i try to insert in the textbox, nothing happens... it doesn't show the sugestion." well i cannot just code all layers here but can suggest in your DAL create a method which returns List and then on your form page provide code like this

 txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
 txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
 var autoCompleteCollection = new AutoCompleteStringCollection();
 autoCompleteCollection.AddRange(DAL.GetMethod().ToArray());
 textbox.AutoCompleteCustomSource = autoCompleteCollection;
James
  • 729
  • 6
  • 10
  • Well, I try to do that but is calling me an error at the 'AutoCompleteCollection.AddRange()' It's say that cannot convert models to string – gabriela-moreira Oct 30 '16 at 19:57
  • Try to return a List from your DAL and call that in your form, like i mentioned, autoCompleteCollection.AddRange(DAL.GetMethod().ToArray()); DAL.GetMethod() should return a List. – James Oct 31 '16 at 03:43
0

Thanks for the help!! I used your suggest and had made some little changes and it work just fine to me...

Turns out that the only problem was my method list, once I change it do a List < String > things got better. For who is wondering, here is how I do it:

DAL LAYER:

public List<string> LoadList()
{
List<string> tagsList = new List<string>();

using (SqlConnection connection = new SqlConnection(ADados.StringDeConexao))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT column FROM table";

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (!reader.IsDBNull(0))
tagsList.Add(reader.GetString(0));
}
reader.Close();
}
connection.Close();
return tagsList;
}

PRESENTATION LAYER (Event TextChanged):

PedidoBLL pedido = new PedidoBLL();

txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection popula = new AutoCompleteStringCollection();
popula.AddRange(pedido.LoadList().ToArray());
txtName.AutoCompleteCustomSource = popula;

In the BLL Layer i just call and return the DAL method LoadList...