Click here to Skip to main content
12,821,494 members (31,507 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET
i want to create autocomplete in c# but want to retrive same name but it only display one name
for eg.

when i want to retrive james
it only give one james
i want to retrive 2 james
how i can do it.
code is.
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.AutoCompleteCustomSource = namesCollection;
            SqlDataReader dReader;
            SqlConnection conn = new SqlConnection("Data Source = .\\SQLEXPRESS; Initial catalog = smart_phone_book; Integrated Security = true");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select distinct Name from smart_book" + " order by Name ";
            dReader = cmd.ExecuteReader();
            if (dReader.HasRows == true)
                while (dReader.Read())

Posted 5-Jul-12 5:58am
Updated 5-Jul-12 6:08am
Wes Aday 5-Jul-12 11:58am
and your string concatenation is not neccesary.
airtil9560919243 6-Jul-12 1:46am
no it is not working please give any other solution
ryanb31 6-Jul-12 7:54am
What does not working mean?
airtil9560919243 6-Jul-12 9:26am
still removing duplicasy
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You are using the keyword DISTINCT which means it will remove duplicates. Just get rid of DISTINCT in your SQL command.
Sandeep Mewara 5-Jul-12 11:09am
5! Ditto.
Shemeer NS 5-Jul-12 14:27pm
airtil9560919243 6-Jul-12 1:44am
no it is not working
ryanb31 6-Jul-12 7:53am
What does that mean?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You have given a query for unique names and hence the result.

cmd.CommandText = "Select Name from smart_book" + " order by Name ";
@amitgajjar 5-Jul-12 12:13pm
why you use + to concat two string? you can simply write in single string.
Sandeep Mewara 5-Jul-12 12:29pm
Good point. I simply copied it and removed distinct for OP.

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Just remove the distinct keyword and it will work fine.
airtil9560919243 6-Jul-12 9:45am
i have removed distinct keyword but still it is giving same problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170308.1 | Last Updated 5 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100