Click here to Skip to main content
15,885,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
C#
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
Updated 5-Jul-12 5:08am
v3
Comments
[no name] 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
ZurdoDev 6-Jul-12 7:54am    
What does not working mean?
airtil9560919243 6-Jul-12 9:26am    
still removing duplicasy

You are using the keyword DISTINCT which means it will remove duplicates. Just get rid of DISTINCT in your SQL command.
 
Share this answer
 
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
ZurdoDev 6-Jul-12 7:53am    
What does that mean?
You have given a query for unique names and hence the result.

Use:
C#
cmd.CommandText = "Select Name from smart_book" + " order by Name ";
 
Share this answer
 
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:
Just remove the distinct keyword and it will work fine.
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900