Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Good Day!
I hope someone can help me, I am trying to create an efficient way of searching records in my database. For Example: I Have Table that contains the list of courses, in my system I will allow users to check if the course they wanted is already in the database.

At first, I am showing the user all the list of available courses and there is a textbox where in they can type words/course that will search from the database and will be displayed in the gridView all matching courses either perfect match or some words only matches.

I tried getting each words using regex and i manage to get each word typed by the user what i want to happen now is to search this words for matching courses in the database. but i can't show the selected data in the gridview. Hope you can help me..

Thank you very much.

VB
Dim myMatches As MatchCollection
        Dim myRegex As New Regex("\w+")
        Dim t As String
        t = TextBox5.Text
        myMatches = myRegex.Matches(t)
        ' Search for all the words in a string
        Dim successfulMatch As Match
        For Each successfulMatch In myMatches
            ListBox1.Items.Add(successfulMatch.Value)
            Dim sql As String = " Select * from Courses_Tbl where Crs_Desc Like ''"& t &"'%' or  Crs_Desc Like '%'"& t &"''"
        Dim adp As New SqlDataAdapter(sql, con)
        Dim com As New SqlCommand(sql, con)
       Dim adp As New SqlDataAdapter(sql, con)
        Dim ds As New DataTable
       adp.Fill(ds)

        Next
        GridView2.DataSource = ds
        GridView2.DataBind()

        Next
        GridView2.DataSource = ds
        GridView2.DataBind()
Posted
Updated 4-Sep-11 15:59pm
v2

First off, I noticed you have used select *.
Do you really need all records returned from the table?

Try and return only those records in the query that are actually needed by your application.

The second thing could be to make sure the table is indexed to ensure better filtering using the where clause.

You might also want to consider the approach of loading all the data once and then filtering on the UI rather than sending a query to the database every single time. This is not always recommended though, and there are pros and cons for this strategy.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Sep-11 0:34am    
Correct, "select *" totally defeats the purpose of the query as such. Why using relationship database at all, only to replace queries by search on client side? Makes no sense.
5 for the solution.
--SA
Abhinav S 5-Sep-11 1:50am    
Thank you SA.
Thank you for your reply Abhinav S. I really appreciate your help. I will review this again.

Thank you.
 
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