2

I am trying to get my textbox to auto-complete when a user types in it to write a query. This would be similar to how SQL Server Management Studio does it and gives the option as you type to arrow down or click on a table name or column name. Here is the following code I have.

public void loadData()
{
    var myConnection = new SqlConnection(DBConnectionBox.Text);
    myConnection.Open();

    AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
    string query = @"Select distinct [name] from [INFORMATION_SCHEMA.TABLES]";

    SqlCommand cmd = new SqlCommand(query, myConnection);

    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.HasRows == true)
    {
        while (dr.Read())
            namesCollection.Add(dr["name"].ToString());
    }

    dr.Close();
    myConnection.Close();

    ManualQueryBox.AutoCompleteMode = AutoCompleteMode.Append;
    ManualQueryBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
    ManualQueryBox.AutoCompleteCustomSource = namesCollection;
}

private void ManualQueryBox_KeyUp(object sender, KeyEventArgs e)
{
    loadData();
}

This is the code I use to grab what is in the textbox and execute it.

private void ExecuteBtn_Click(object sender, EventArgs e)
{
    this.ClientInfoDGV.DataSource = null;
    this.ClientInfoDGV.Rows.Clear();

    var myConnection = new SqlConnection(DBConnectionBox.Text);

    var ManualCmd = new SqlCommand(ManualQueryBox.Text);
    ManualCmd.Connection = myConnection;
    ManualCmd.CommandType = CommandType.Text;

    SqlDataAdapter SqlAdap = new SqlDataAdapter(ManualCmd);
    DataTable MQRecord = new DataTable();
    SqlAdap.Fill(MQRecord);

    ClientInfoDGV.DataSource = MQRecord;
}

I have never done autofill before but looking around I have see some asp.net AJAX control toolkit, but I am not entirely sure how that all works. Any help is welcome.

Update to auto-fill code

public void loadData()
        {
            var myConnection = new SqlConnection(DBConnectionBox.Text);
            myConnection.Open();
            AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
            string query = @"Select distinct [Id] from [Clients]";
            SqlCommand cmd = new SqlCommand(query, myConnection);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                while (dr.Read())
                    namesCollection.Add(dr["id"].ToString());

            }

            dr.Close();
            myConnection.Close();

            ManualQueryBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            ManualQueryBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
            ManualQueryBox.AutoCompleteCustomSource = namesCollection;
        }

1 Answer 1

2

Try changing autocomplete mode to suggestappend:

ManualQueryBox.AutoCompleteMode=AutoCompleteMode.SuggestAppend;

I don't think you need to load the data every time in the key up event as long as you load it once (perhaps in form load) and specify the source of the text box.

This might help:https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

9 Comments

It isn't liking my query. Any suggestions on what query to run to obtain what I am looking for?
invalid object name INFORMATION_SCHEMA.TABLES. I wasn't sure what to put in the string query portion so I just put in a piece of code that would grab the table tables for the database you are connected to.
Do you have the north wind sample db? It might be easier to do a select from a regular data table like employees for instance.
Not entirely sure what you mean by that.
Then just use any table in your db.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.