Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

retrieve data from database using jquery autocomplete

Hello Experts,

I would like to have values from my database get autocompleted as I type in a TextBox control. I'm having trouble finding good examples of this online. Could really use some help.
Avatar of Russ Suter
Russ Suter

If you're talking about a relatively small number of values then you should pull them into memory ahead of time. If you have a lot of records then you may be looking at a large number of queries in a short time which isn't an especially good idea.

Can you give me some idea of how many records you're looking at retrieving? There are ways to do this but the question is if it's a good idea or not.
Avatar of Brian

ASKER

I would say it would be around 150 - 300 records.
For a list that small I'd just query the database once at page load and store the values in memory.
Avatar of Brian

ASKER

Ok, can you help with that. That part I already understand.
ASKER CERTIFIED SOLUTION
Avatar of Monica P
Monica P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

@AkilaPalanimuthu,

Thank you for the link. That worked out great. I have just a few small questions to ask though before closing this post.

1.) How can I use a Stored Procedure instead of an SQL Statement as below?

    [WebMethod]
    public static List<string> GetAutoCompleteData(string username)
    {
        List<string> result = new List<string>();

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CMDB"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("select DISTINCT product_name from ProductNumbers where product_name LIKE '%'+@SearchText+'%'", conn))
            {
                conn.Open();
                cmd.Parameters.AddWithValue("@SearchText", username);

                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    result.Add(dr["product_name"].ToString());
                }

                return result;
            }
        }
    }

Open in new window


2.) Is there a way to have another value get populated based on the selected value from the AutoComplete list? So for example. I have a textbox called Product Number that is getting retrieved using autocomplete. Once a user selects a value from autocomplete I then need to populate the value related to the selected autocomplete value to be returned to a label control. The values in the database are related I jsut don't know how to return the other value based on the selected value from the autocomplete.

I would be MORE than willing to create another ticket if you think you can assist me with that.