Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
like i need data from pname which is hassan but i want to write hasan or hsssan or any word start with h in textbox2 code must chack first spell of textbox2 and find the related values.

and want to show them in datagridview

i tried the above code but it search the value start from textbox2 value

like if i want hassan then i need to enter h.
but i want to write the wrong spell but the first spell must be correct.

What I have tried:

SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM stock  WHERE pname LIKE @pname", con);
              sqlDa.SelectCommand.Parameters.AddWithValue("@pname", textBox2.Text + "%");
Posted
Updated 22-Oct-19 8:03am

There isn't a magic option to find the "correct" spelling of the user's search term. And even if there was, there's no guarantee that the data in the database would be spelled "correctly" anyway.

The closest you can get is to calculate the difference between the soundex[^] codes for the database value and the search term. SQL Server has a built-in function to do that, which returns a value from 0 to 4, where 0 means not at all similar, and 4 means virtually identical.
C#
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM stock  WHERE DIFFERENCE(pname, @pname) > 1", con);
sqlDa.SelectCommand.Parameters.AddWithValue("@pname", textBox2.Text);
DIFFERENCE (Transact-SQL) - SQL Server | Microsoft Docs[^]

NB: Soundex is specifically based on English pronunciation. Depending on your users, your results may vary.

There are other methods for comparing similar-sounding strings, but none that are built in to SQL Server. For example, PostgreSQL offers:
PostgreSQL: Documentation: 9.1: fuzzystrmatch[^]
 
Share this answer
 

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