Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB.NET
I am trying to make a search bar. So far this code works if I type the exact words of the information I want to search, but what I want to do is to at least detect 8-10 characters from what I typed in the search bar and the results would show.
 
Also.. I am having problems when searching for number data types. It won't let me search information with numbers in it. Only texts..
 
Any idea why?
 
 connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\lppUni.mdb;"
        Dim ds As New DataSet
        Dim sql As String = "Select * from LPPUN where studID = '" & TextBox1.Text & "' OR studName = '" & TextBox1.Text & "' OR studCourse = '" & TextBox1.Text & "'"
        Dim dt As DataTable
        cnn = New OleDbConnection(connectionString)
 
        Try
            cnn.Open()
            adptr = New OleDbDataAdapter(sql, cnn)
            adptr.Fill(ds)
            adptr.Dispose()
            cnn.Close()
 
            dt = ds.Tables(0)
            studformGridView.DataSource = dt
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
 
        studformGridView.Visible = True
    End Sub
Posted 18-Mar-13 3:21am
Semeki259
Edited 18-Mar-13 3:22am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use Like in place of =
 
"Select * from LPPUN where studID Like '%" & TextBox1.Text +"%' OR studName Like '%" & TextBox1.Text + "%' OR studCourse Like '%" & TextBox1.Text + "%'"
 
and your search will return with matching characther.
  Permalink  
Comments
Semeki at 18-Mar-13 11:36am
   
Oh it doesn't return anything unless you type the exact word that you are searching for :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As with the prev Solution, use "like" but also change the "+" with "&"
 
e.g.
 
"Select * from LPPUN where studID Like '%" & TextBox1.Text & "%' OR studName Like '%" & TextBox1.Text & "%' OR studCourse Like '%" & TextBox1.Text & "%'"
 
Also, watch out for intentional / non-intentional SQL injection with your textbox values
 
Smile | :)
  Permalink  
Comments
Semeki at 18-Mar-13 12:53pm
   
Thank you very much, it's working now! But I'm wondering if we can do the same thing if the data type is in Integer? Because it doesn't seem to work.
bigcdh at 18-Mar-13 12:56pm
   
should be exactly the same... whats the error you get?
Semeki at 19-Mar-13 7:06am
   
There is no error but it the data grid view doesn't show any data at all.
bigcdh at 19-Mar-13 7:10am
   
Hmm, well shouldnt be any reason why it wouldnt work that I can see.
 
Try taking the try / catch out and see if it throws an error which may give you more of an indication.
 
Also, just out of interest, you dont need to use cnn.Open() or cnn.Close() as when filling an adapter with a dataset this is already taken care of. Just a couple less lines of code :-)
Semeki at 19-Mar-13 23:13pm
   
Okay it's working now! Thank you very much for helping me! :D
bigcdh at 20-Mar-13 4:58am
   
pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100