VB.NET - how to use like operator in vb.net - Asked By Muralikrishnan on 28-May-10 03:15 AM

i need sample to use " like " operator in vb.net

thx in advance
Sasha Kotlo replied to Muralikrishnan on 28-May-10 03:21 AM
if you're talking about SQL server's LIKE than for strings you have the Contains() method.

Regards.
Mash B replied to Muralikrishnan on 28-May-10 03:28 AM
Example
 
This example uses the Like operator to compare a string to a pattern. The result is a Boolean value representing whether the string fits the pattern.
 
 
Dim myCheck As Boolean
myCheck = "F" Like "F"   ' Does "F" match "F"? Returns True.
myCheck = "F" Like "f"   ' Does "F" match "f"? Returns False.
myCheck = "F" Like "FFF"   ' Does "F" match "FFF"? Returns False.
myCheck = "aBBBa" Like "a*a"   ' Does "aBBBa" have a "a" at the
   ' beginning, an "a" at the end, and any number of characters in
   ' between? Returns True.
myCheck = "F" Like "[A-Z]"   ' Does "F" occur in the set of
   ' characters from A to Z? Returns True.
myCheck = "F" Like "[!A-Z]"     ' Does "F" NOT occur in the set of
   ' characters from A to Z? Returns False.
myCheck = "a2a" Like "a#a"     ' Does "a2a" begin and end with an
   ' "a" and have any single-digit number inbetween? Returns True.
myCheck = "aM5b" Like "a[L-P]#[!c-e]" ' Does "aM5b" fit the following
   ' pattern: Begins with "a", has and character from the set L through
   ' P, followed byb any single-digit number, and finally contains any
   ' character excluded from the character set c through e. Returns True.
myCheck = "BAT123khg" Like "B?T*"  ' Does "BAT123khg" fit the
   ' following pattern: Begins with "B", followed by any single
   ' character, followed by a "T" and finally zero or more characters
   ' of any type. Returns True.
myCheck = "CAT123khg" Like "B?T*"  ' Does "CAT123khg" fit the
   ' following pattern: Begins with "B", followed by any single
   ' character, followed by a "T" and finally zero or more characters
   ' of any type. Returns False.

For more information visit below site

http://msdn.microsoft.com/en-us/library/swf8kaxw%28vs.71%29.aspx
Anoop S replied to Muralikrishnan on 28-May-10 03:40 AM
The VB.NET ‘Like’ operator compares a string to a pattern returning True if the pattern is found in the string or False if the pattern is not found in the string.

a string containing the upper case A character is compared to a pattern containing the upper case F character.

Dim pattern1 As String = "A"
Console.WriteLine(("A" Like pattern1).ToString)

RESULT:  True

 
Muralikrishnan replied to Mash B on 28-May-10 03:44 AM

cmd =

New SqlCommand(" select admission_no,doa,student_id,student_name,class,sec from classadmitted where student_name like '%" & Trim(txtstudent.Text) & "%' ", con)

If con.State = ConnectionState.Closed Then con.Open()

da =

New SqlDataAdapter(cmd)

ds =

New DataSet

da.Fill(ds,

"classadmitted")

DGV_f1datasfromclasdb.DataSource = ds.Tables(

"classadmitted").DefaultView

this s my code but if i type half name in the text box its not displaying the result..

eg: The table has student name has MuraliBR , if i type murali in the textbox to search its not displaying ..

Super Man replied to Muralikrishnan on 28-May-10 08:46 AM

Like operator in vb.net

It takes two operator as string and check if they are same then it returns true otherwise it returns false.

 

While in sql,

Like operator is used in use with wildcard character;

Ex.

Select * from users where fname like ‘a%’;

o/p:

 

asin

aesan