Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
if i will put one sentence in one textbox that all the words will check one by one like google search.and it will display all related datas(means Ex-If i will put one sentence 'Hi ! welcome to india' then it will display all related data by checking every words in database.the result can display all the related data means in the word india means it can display all related 'in' or 'ind ia' or 'nd' in datagridview ) in datagridview.In my following coding it is spiliting the sentence and displaying all the data of particular word but it is not displaying the related data of related word means if i will take 'wellcome to india' then it should filtrate the datas of 'in' or 'dia'.

My Coding is:

C#
private void btnSearch_Click(object sender, EventArgs e)
{
    try
    {
        string commaDelimited = textBox1.Text;
        string[] year = commaDelimited.Split(new char[] { });
        /////////////////////////////////////////
        //string[] r = { "TV", "Radio", "Coal" };
        string filter = string.Empty;
        foreach (string name in year)
        {
            if (filter.Length > 0)
            {
                filter += ",";
            }
            filter += string.Format("'{0}'", name);
        }
        SqlDataAdapter adp;
        DataSet ds = new DataSet();
        adp = new SqlDataAdapter("Select * from EMP_DET where FName IN (Like '%(" + filter + ")%')", con);
        adp.Fill(ds, "EMP_DET");
        dataGridView1.DataSource = ds.Tables[0].DefaultView;
    }
    catch
    { }
}
Posted
Updated 5-Apr-11 23:41pm
v2
Comments
JOAT-MON 6-Apr-11 5:42am    
wrapped code block
Ankur\m/ 6-Apr-11 5:59am    
"Select * from EMP_DET where FName IN (Like '%(" + filter + ")%')"

This part is wrong. It doesn't work like this. Search Google for dynamic sql and do some research on how to pass such comma separated parameter.

1 solution

The SQL LIKE operator does not work like that - see here: http://www.w3schools.com/sql/sql_like.asp[^]

Instead, you need to build your command with a range of conditions:
SQL
SELECT * FROM EMP_DET WHERE FName LIKE %word1% OR FName LIKE %word2%
 
Share this answer
 
Comments
Banajyotsna 6-Apr-11 10:16am    
But in my query filter contains the splitted words of sentence putted through the textbox.which is matching one by one words of that sentence and retrieving the datas.my question how it will retrieve the related data of word(i want to put Like keyword to match the latter of those splitted word).please try to understand my query.retrieve datas from database by matching latter of those splitted word.Ex-google search.I think it will succeed if we will use IN and LIKE sql commands in one SQLquery.But i do not know how to use in a single query
OriginalGriff 6-Apr-11 10:43am    
You can't. The SQL IN operator only works with a list of values: LIKE does not return such.
It *may* be possible with a stored procedure to return a list of all records that are LIKE a word, and convert them into a list suitable for handing to IN, but it's a nasty way to do it!

Instead, change your filter generation to generate the "FName LIKE %word%" clauses instead of comma separating them.

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