Click here to Skip to main content
12,299,012 members (51,556 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.
id=1
name=james
id=2
name=james

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 ";
conn.Open();
dReader = cmd.ExecuteReader();
if (dReader.HasRows == true)
{
    while (dReader.Read())
        namesCollection.Add(dReader["Name"].ToString());
 
}
Posted 5-Jul-12 4:58am
Edited 5-Jul-12 5:08am
v3
Comments
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
 
good
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.
  Permalink  
Comments
Sandeep Mewara 5-Jul-12 11:09am
   
5! Ditto.
Shemeer NS 5-Jul-12 14:27pm
   
+5
airtil9560919243 6-Jul-12 1:44am
   
no it is not working
ryanb31 6-Jul-12 7:53am
   
What does that mean?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

Use:
cmd.CommandText = "Select Name from smart_book" + " order by Name ";
  Permalink  
Comments
@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.

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

Solution 3

Just remove the distinct keyword and it will work fine.
  Permalink  
Comments
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
Web02 | 2.8.160525.2 | Last Updated 5 Jul 2012
Copyright © CodeProject, 1999-2016
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