C# .NET - C# Search Help ! - Asked By ladey pink on 27-Jan-10 03:32 AM

Hi.
I need help to search names starting with the alphabet keyed in onto the textbox.
Which means, if I key in "A" in the textbox, names starting with "A" will be displayed. 
I can't use "%" can't I? 
And should I include LIKE as well?

Please help.
Thanks.

Select empID from tblemployee where empname Like 'A%' - Kalit Sikka replied to ladey pink on 27-Jan-10 03:42 AM

Select empID from tblemployee where empname Like 'A%'

replied - ladey pink replied to Kalit Sikka on 27-Jan-10 03:43 AM

What if I want to key in other alphabets, what should I put in my codes?

C# Search - Shailendrasinh Parmar replied to ladey pink on 27-Jan-10 03:44 AM

Yes you should include LIKE keyword in SELECT query to search the name starting with "A".
And the query will become like this.

Select * from tablename where columnName LIKE 'A%',

It will return all the rows where columnName value starts with 'A'.

If you don't want to use '%', then you can use '_' also, but it will be for fixed characters. 

Like say, if you are searching for name 'Anil' then you have to write the query like this.

Select * from tablename where columnName LIKE 'A___', (after A, there are 3 '_' characters).

Hope this helps.
re - Santhosh N replied to ladey pink on 27-Jan-10 03:47 AM

Its like you need to implement this in key press event of the textbox and get the textbox value and append % at the end and send to the DB and on DB end you need to use Like operator

in key press event

string val = TextBox1.Text + "%";

Send this as a parameter to the SP or you can directly call as a query

Select name from table1 where upper(uname) like upper(val)

see - Kalit Sikka replied to ladey pink on 27-Jan-10 03:47 AM
txtSearch is textbox on form that will accept a required char.
string s = txtSearch.Text + "%";
string sql = Select empID from tblemployee where empname Like 's' // pass the value of s here.........


Correction - Kalit Sikka replied to Santhosh N on 27-Jan-10 03:48 AM
txtSearch is textbox on form that will accept a required char.
string s = txtSearch.Text + "%";
string sql = Select empID from tblemployee where empname Like '"+s+"' // pass the value of s here.........
mv ark replied to ladey pink on 29-Jan-10 07:30 AM
This code sample (with the query) shows how you can fetch records matching characters of a word as each is being typed while using the ASP.NET AJAX Autocomplete Extender  - http://www26.brinkster.com/mvark/codegallery/autocomplete.aspx